androidandroid-intentsmsdual-sim

Send sms using sim selection option


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.

My Program

public class MainActivity extends AppCompatActivity {

Context mContext = MainActivity.this ;

String SENT = "SMS_SENT";

String DELIVERED = "SMS_DELIVERED";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

sendSMS("XXXXXXXX77","Sent by Dual Sim Support Testing");
}

public void sendSMS(final String paramString1,final String paramString2)
{
final PendingIntent localPendingIntent1 = PendingIntent.getBroadcast(mContext, 0, new Intent(this.SENT), 0);
final PendingIntent localPendingIntent2 = PendingIntent.getBroadcast(mContext, 0, new Intent(this.DELIVERED), 0);

Log.v("ppi", "DUal SIm:Build.VERSION.SDK_INT:" + Build.VERSION.SDK_INT);

if (Build.VERSION.SDK_INT >= 22)
{
SubscriptionManager localSubscriptionManager = SubscriptionManager.from(mContext);
if (localSubscriptionManager.getActiveSubscriptionInfoCount() > 1)
{
List localList = localSubscriptionManager.getActiveSubscriptionInfoList();
final String[] arrayOfString = new String[localList.size()];
int i = 0;
Iterator localIterator = localList.iterator();
while (localIterator.hasNext())
{
SubscriptionInfo localSubscriptionInfo = (SubscriptionInfo)localIterator.next();
localSubscriptionInfo.getSubscriptionId();
//log.d("22 api level ", "got dual sim: ");
int j = i + 1;
arrayOfString[i] = (localSubscriptionInfo.getCarrierName().toString() + " " + localSubscriptionInfo.getNumber());
i = j;
}

Log.v("ppi","DUal SIm:arrayOfString:"+arrayOfString.length);

for(i = 0; i < arrayOfString.length; i++ ){

Log.v("ppi","DUal SIm:arrayOfString:"+arrayOfString[i]);
}

final AlertDialog.Builder localBuilder = new AlertDialog.Builder(mContext);
localBuilder.setTitle("Choose SIM ");
localBuilder
.setCancelable(false)
.setPositiveButton("SIM1",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {

SmsManager.getDefault().sendTextMessage(paramString1, null, paramString2, localPendingIntent1, localPendingIntent2);

//dialog.cancel();
}
})
.setNegativeButton("SIM2", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, just close
// the dialog box and do nothing
SmsManager.getDefault().sendTextMessage(paramString1, null, paramString2, localPendingIntent1, localPendingIntent2);

// dialog.cancel();
}
});

localBuilder.setCancelable(false);
localBuilder.show();
}
}
SmsManager.getDefault().sendTextMessage(paramString1, null, paramString2, localPendingIntent1, localPendingIntent2);
}
}

Solution

  • 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
    }
    

    }