I have tried Android SDK above 5.1 OS. I want to send sms using SIM selection option. I tried this Program. It\'s taking sim1 only. It does not take second sim.
Finally, I found exact Sim Selection Option for above android os version 5.1 and above.
public void sendDualSimSMSOption(final String ToaddrmobileNo, final String message) {
// detect os is below 22 or not
if (Build.VERSION.SDK_INT >= 22) {
final PendingIntent PendingIntent1 = PendingIntent.getBroadcast(mContext, 0, new Intent(this.SENT), 0);
final PendingIntent PendingIntent2 = PendingIntent.getBroadcast(mContext, 0, new Intent(this.DELIVERED), 0);
SubscriptionManager localSubscripManager = SubscriptionManager.from(mContext);
ArrayList<Integer> saveSubscripID = new ArrayList<Integer>();
ArrayList<String> saveSimOperatorNames = new ArrayList<String>();
ArrayList<String> saveSimSerialNumbers = new ArrayList<String>();
final int[] subscripInformationArray = new int[2];
if (localSubscripManager.getActiveSubscriptionInfoCount() > 1) {
List localList = localSubscripManager.getActivesubscripInformationList();
final String[] arrayOfStringData = new String[localList.size()];
int i = 0;
Iterator localIterator = localList.iterator();
while (localIterator.hasNext()) {
SubscriptionInfo localSubscriptionInfo = (SubscriptionInfo) localIterator.next();
localSubscriptionInfo.getSubscriptionId();
Log.v("SIM", "22 api level..got dual sim: ");
int j = i + 1;
String SimCarrierName = (localSubscriptionInfo.getCarrierName().toString().trim());
String SimDisplayName = (localSubscriptionInfo.getDisplayName().toString().trim());
String SimSerialNo = (localSubscriptionInfo.getIccId().toString().trim());
if( SimCarrierName != null && SimCarrierName.length() > 0)
{
arrayOfStringData[i] = SimCarrierName ;
Log.v('SIM', "SIM.getCarrierName:", arrayOfStringData[i]);
}
else {
arrayOfStringData[i] = SimDisplayName ;
Log.v('SIM', "SIM.SimDisplayName:", arrayOfStringData[i]);
}
i = j;
}
SubscriptionManager subscriptionManager = SubscriptionManager.from(getApplicationContext());
List<SubscriptionInfo> subscripInformationList = subscriptionManager.getActivesubscripInformationList();
for (SubscriptionInfo subscriptionInfo : subscripInformationList) {
int subscriptionId = subscriptionInfo.getSubscriptionId();
Log.v('SIM', "SIM.subscriptionId:",""+subscriptionId);
}
for (i = 0; i < subscripInformationList.size(); i++) {
Log.v("SIM", "DUal SIm:subscripInformationList:" + subscripInformationList.get(i));
Log.v("SIM", "DUal SIm:getSubscriptionId:" + subscripInformationList.get(i).getSubscriptionId());
subscripInformationArray[i] = subscripInformationList.get(i).getSubscriptionId();
saveSubscripID.add(subscripInformationList.get(i).getSubscriptionId());
saveSimSerialNumbers.add(subscripInformationList.get(i).getIccId());
}
for (i = 0; i < arrayOfStringData.length; i++) {
Log.v("SIM", "DUal SIm:arrayOfStringData:" + arrayOfStringData[i]);
Log.v("SIM", "DUal SIm:subscripInformationArray:"+ subscripInformationArray[i]);
if( arrayOfStringData[i] != null && arrayOfStringData[i].length() > 0)
{
saveSimOperatorNames.add(arrayOfStringData[i]);
}
else{
saveSimOperatorNames.add("Sim "+(i+1));
}
}
// this is the method to show sim selection dialog with above sim serials info.
SimSelection_ShowDialogBox(context, ToAddrMobNo, message,saveSubscripId, saveSimOperaNames,saveSimSerialNumbers);
}
}
else{
// else part. default sim taking to send sms
}
}
I looked into SubscriptionManager class and found a method to get subsCription info for perticular sim slot
This worked for me:
public void sendSMS(final String number,final String text)
{
final PendingIntent localPendingIntent1 = PendingIntent.getBroadcast(mContext, 0, new Intent(this.SENT), 0);
final PendingIntent localPendingIntent2 = PendingIntent.getBroadcast(mContext, 0, new Intent(this.DELIVERED), 0);
if (Build.VERSION.SDK_INT >= 22)
{
SubscriptionManager subscriptionManager=((Activity)mContext).getSystemService(SubscriptionManager.class);
SubscriptionInfo subscriptionInfo=subscriptionManager.getActiveSubscriptionInfoForSimSlotIndex(simIndex);
SmsManager.getSmsManagerForSubscriptionId(subscriptionInfo.getSubscriptionId()).sendTextMessage(number, null, text, localPendingIntent1, localPendingIntent2);
}
SmsManager.getSmsManagerForSubscriptionId(subscriptionInfo.getSubscriptionId()).sendTextMessage(number, null, text, localPendingIntent1, localPendingIntent2);
}
Where simIndex is 0 for sim1 and 1 for sim2
That is because you have:
SmsManager.getDefault()
which is SIM1, instead of having:
SmsManager.getSmsManagerForSubscriptionId(int subscriptionId)
when sending SMS in your dialog buttons click events.