-6

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?

Priyanka Singhal
  • 243
  • 1
  • 4
  • 20
  • Can i know what is unclear in this? – Priyanka Singhal Jun 14 '18 at 06:10
  • Please read the Stackoverflow "how to ask" here: https://stackoverflow.com/help/how-to-ask To me your question is bad quality because you don't show what you have tried and what exact problem you face. Stackoverflow is to help you with a certain problem not to provide you with complete solutions. Again please read "how to ask". – Bruno Bieri Jun 14 '18 at 06:22
  • 1
    @priyankasinghal This platform is used to get help from others if we are having difficulties in the code. This isn't used to get the source code or help to build. Please read the link https://stackoverflow.com/help/how-to-ask to know more about the platform. – Abhishek D Jun 14 '18 at 06:27
  • I have added my code. Is it okay now? – Priyanka Singhal Jun 14 '18 at 07:05

1 Answers1

1

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);
    }
double-beep
  • 5,031
  • 17
  • 33
  • 41
Ranjith KP
  • 858
  • 6
  • 18