Option to send sms using Sim1 or Sim2 programmatically Option to send sms using Sim1 or Sim2 programmatically android android

Option to send sms using Sim1 or Sim2 programmatically


If you can use this code for API level 22+.

private void sendDirectSMS() {    private static String SENT = "SMS_SENT", DELIVERED = "SMS_DELIVERED";    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(        SENT), 0);    PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,        new Intent(DELIVERED), 0);    // SEND BroadcastReceiver    BroadcastReceiver sendSMS = new BroadcastReceiver() {        @Override        public void onReceive(Context arg0, Intent arg1) {            switch (getResultCode()) {                case Activity.RESULT_OK:                    showSnackBar(getString(R.string.sms_sent));                    Analytics.track(AnalyticsEvents.SEND_REMINDER_SMS_APP_SUCCESS);                    break;                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:                    showSnackBar(getString(R.string.sms_send_failed_try_again));                    Analytics.track(AnalyticsEvents.SEND_REMINDER_SMS_APP_FAILED);                    break;                case SmsManager.RESULT_ERROR_NO_SERVICE:                    showSnackBar(getString(R.string.no_service_sms_failed));                    Analytics.track(AnalyticsEvents.SEND_REMINDER_SMS_APP_FAILED);                    break;                case SmsManager.RESULT_ERROR_NULL_PDU:                    showSnackBar(getString(R.string.no_service_sms_failed));                    Analytics.track(AnalyticsEvents.SEND_REMINDER_SMS_APP_FAILED);                    break;                case SmsManager.RESULT_ERROR_RADIO_OFF:                    showSnackBar(getString(R.string.no_service_sms_failed));                    Analytics.track(AnalyticsEvents.SEND_REMINDER_SMS_APP_FAILED);                    break;            }        }    };    // DELIVERY BroadcastReceiver    BroadcastReceiver deliverSMS = new BroadcastReceiver() {        @Override        public void onReceive(Context arg0, Intent arg1) {            switch (getResultCode()) {                case Activity.RESULT_OK:                    Toast.makeText(getBaseContext(), R.string.sms_delivered,                        Toast.LENGTH_SHORT).show();                    break;                case Activity.RESULT_CANCELED:                    Toast.makeText(getBaseContext(), R.string.sms_not_delivered,                        Toast.LENGTH_SHORT).show();                    break;            }        }    };    registerReceiver(sendSMS, new IntentFilter(SENT));    registerReceiver(deliverSMS, new IntentFilter(DELIVERED));    String smsText = getSmsText();    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {        SubscriptionManager localSubscriptionManager = SubscriptionManager.from(context);        if (localSubscriptionManager.getActiveSubscriptionInfoCount() > 1) {            List localList = localSubscriptionManager.getActiveSubscriptionInfoList();            SubscriptionInfo simInfo1 = (SubscriptionInfo) localList.get(0);            SubscriptionInfo simInfo2 = (SubscriptionInfo) localList.get(1);            //SendSMS From SIM One            SmsManager.getSmsManagerForSubscriptionId(simInfo1.getSubscriptionId()).sendTextMessage(customer.getMobile(), null, smsText, sentPI, deliveredPI);            //SendSMS From SIM Two            SmsManager.getSmsManagerForSubscriptionId(simInfo2.getSubscriptionId()).sendTextMessage(customer.getMobile(), null, smsText, sentPI, deliveredPI);        }    } else {        SmsManager.getDefault().sendTextMessage(customer.getMobile(), null, smsText, sentPI, deliveredPI);        Toast.makeText(getBaseContext(), R.string.sms_sending, Toast.LENGTH_SHORT).show();    }}

Also add permission.

<uses-permission android:name="android.permission.SEND_SMS"/><uses-permission android:name="android.permission.READ_PHONE_STATE"/>


SmsManager.getSmsManagerForSubscriptionId(int subscriptionId).sendTextMessage(String destinationAddress, String scAddress, String text,PendingIntent sentIntent, PendingIntent deliveryIntent);

this can be working after 5.1 Lollipop version.before that we need to see more. once i got the answer i will update.


You should check out tasker, you would be able to user it to build a toggle widget for that.