androidsmsmanagerdual-sim

Android send sms in Dual SIM Phone


I am working with app which includes sending of sms directly using SIM 1 or SIM 2. It does not matter which SIM is set by default Please help. Thanks

For example, If SIM 1 is set by default and i need to send sms from sim 2 how can it be done.

I tried this

private void SendSMS(final String phoneNo, final String msg) {
        sentStatusReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                String s = "Unknown Error";
                boolean success = false;
                switch (getResultCode()) {
                    case Activity.RESULT_OK:
                        success = true;
                        s = "Message Sent Successfully !!";
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        s = "Generic Failure Error";
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        s = "Error : No Service Available";
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        s = "Error : Null PDU";
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        s = "Error : Radio is off";
                        break;
                    default:
                        break;
                }
                Toast.makeText(context_, s, Toast.LENGTH_SHORT).show();
                getContext().unregisterReceiver(sentStatusReceiver);
                if (success) {
                    SmsActionDialog.this.dismiss();
                } else {
                    ShowAlertBox("I tried to send SMS but failed", "The SMS app will be opened to send the message.Continue?", msg);
                }
            }
        };
        getContext().registerReceiver(sentStatusReceiver, new IntentFilter("SMS_SENT"));
        try {
            final SmsManager smsManager = SmsManager.getDefault();
            final PendingIntent sentIntent = PendingIntent.getBroadcast(getContext(), 0, new Intent("SMS_SENT"), 0);
//            if (msg.length() > 160) {
//                final ArrayList<String> messagelist = smsManager.divideMessage(msg);
//                final ArrayList<PendingIntent> sentPendingIntents = new ArrayList<>();
//                for (int i = 0; i < messagelist.size(); i++) {
//                    sentPendingIntents.add(i, sentIntent);
//                }
//
//                smsManager.sendMultipartTextMessage(phoneNo, null, messagelist, sentPendingIntents, null);
//            } else {
            smsManager.sendTextMessage(phoneNo, null, msg, sentIntent, null);
//            }
        } catch (Exception ex) {
            Toast.makeText(context_, ex.getMessage(), Toast.LENGTH_LONG).show();
            ShowAlertBox("I tried to send SMS but failed", "The SMS app will be opened to send the message.Continue?", msg);
            ex.printStackTrace();
        }
    }

Some devices face generic failure error. How can this be resolved?


Solution

  • Try the following method where simIndex is 0 for sim1 and 1 for sim2:

     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);
        }