HI everyone!
Im using this code to check for dual sim and send sms using dual sim card. I just want to know how i can get the result of my messages sent using PendingIntent.I can already display the result using toast but i need to capture the said event. I want to know if i have sent the message successfully or an error was encountered and capture it using b4a code:
Im using this code to check for dual sim and send sms using dual sim card. I just want to know how i can get the result of my messages sent using PendingIntent.I can already display the result using toast but i need to capture the said event. I want to know if i have sent the message successfully or an error was encountered and capture it using b4a code:
B4X:
#If JAVA
import java.util.List;
import java.util.ArrayList;
import android.app.Activity;
import android.telephony.SubscriptionManager;
import android.telephony.SubscriptionInfo;
import android.telephony.SmsManager;
import android.content.Intent;
import android.content.IntentFilter;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.widget.Toast;
private static final String SMS_SENT = "SMS_SENT";
private static final String SMS_DELIVERED= "SMS_DELIVERED";
public static ArrayList<Integer> sim(Activity activity) {
ArrayList<Integer> simCardList = new ArrayList();
SubscriptionManager subscriptionManager = SubscriptionManager.from(activity);
List<SubscriptionInfo> subscriptionInfoList = subscriptionManager.getActiveSubscriptionInfoList();
for (SubscriptionInfo subscriptionInfo : subscriptionInfoList) {
int subscriptionId = subscriptionInfo.getSubscriptionId();
simCardList.add(subscriptionId);
}
return simCardList;
}
public static ArrayList<String> network(Activity activity) {
ArrayList<String> simCardList = new ArrayList();
SubscriptionManager subscriptionManager = SubscriptionManager.from(activity);
List<SubscriptionInfo> subscriptionInfoList = subscriptionManager.getActiveSubscriptionInfoList();
for (SubscriptionInfo subscriptionInfo : subscriptionInfoList) {
String subscriptionId = subscriptionInfo.getCarrierName().toString();
simCardList.add(subscriptionId);
}
return simCardList;
}
public void sendSMS(Activity activity,Integer simID,String strDest,String strMsg) {
ArrayList<Integer> simCardList = new ArrayList();
SubscriptionManager subscriptionManager = SubscriptionManager.from(activity);
List<SubscriptionInfo> subscriptionInfoList = subscriptionManager.getActiveSubscriptionInfoList();
for (SubscriptionInfo subscriptionInfo : subscriptionInfoList) {
int subscriptionId = subscriptionInfo.getSubscriptionId();
simCardList.add(subscriptionId);
}
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SMS_SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(SMS_DELIVERED), 0);
//---when the SMS has been sent---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS sent",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SMS_SENT));
//---when the SMS has been delivered---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SMS_DELIVERED));
int smsToSendFrom = simCardList.get(simID); //assign your desired sim to send sms, or user selected choice
SmsManager.getSmsManagerForSubscriptionId(smsToSendFrom).sendTextMessage(strDest, null, strMsg, sentPI, deliveredPI); //use your phone number, message and pending intents
}
#End If