22

I am working with SMS Manager for sending sms in android.The code i am using is as below:

private void sendSms(String Phnno, String Message) {
    if (Utils.checkSIM(MyActivity.this)) {
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(Phnno, null, Message, null, null);
        Utils.showMessage(MyActivity.this,"","Thank You for your interest,please check your inbox for the results.");

    } else {
        showErrorMessage(MyActivity.this, "SIM Error!",
                "Please insert SIM");
    }

}

This code works for me perfectly on single sim phones but when i check this in dual sim phones i am getting following warning and SMS never sends.

01-11 15:56:13.664: W/sendTextMessage(6656): use single sim interface to sendTextMessage by double sim interface

Please suggest how i can achieve it on my dual sim phone.Thanks in Advance.

Prabhjot Singh
  • 526
  • 1
  • 3
  • 17
  • how did you manage to get the scaddress ? – John x Dec 23 '13 at 09:57
  • see this link its work for me on huawei g730 [http://stackoverflow.com/a/30677542/5102893](http://stackoverflow.com/a/30677542/5102893) – SHADOW NET Jan 06 '16 at 05:14
  • I would highly recommend this much cleaner solution (no reflection needed, works for API level 22+) found here https://stackoverflow.com/a/51380282/3427883 – Abdu Sep 20 '18 at 22:19

3 Answers3

8

This Will work for both scenario. If Already user has been selected the default sim it will automatically takes that and goto the next process, Otherwise while click on the send button it will ask the confirmation for choose any sim to send the sms. we have tested its working fine.

Sample Source Code:

try 
{    
     Intent sendIntent = new Intent(Intent.ACTION_VIEW);
     sendIntent.putExtra("sms_body","Body");
     sendIntent.putExtra("address", "PhoneNumber");
     sendIntent.setType("vnd.android-dir/mms-sms");
     startActivity(sendIntent);
} 
catch (Exception e) 
{
     Toast.makeText(getApplicationContext(),"SMS faild, please try again later!",Toast.LENGTH_SHORT).show();
     e.printStackTrace();
}
Prasanth S
  • 3,725
  • 9
  • 43
  • 75
  • 11
    This might work correctly. But wouldn't this first redirect to SMS create screen? What if he wants to send message directly? – SKT Aug 08 '13 at 10:53
  • 4
    This code will start the SMS send app. Not good for background SMS sending. – allprog Aug 15 '13 at 09:18
  • 8
    The answer is not directly addressing the code in question. This is just a workaround. – Saran Oct 21 '13 at 16:20
4

sendTextMessage() has the scAddress parameter. This is used to define the SMS center address. I think if you set it correctly, you'll be able to send a message.

You can find the number by following this tutorial: http://algorithmic-indian.blogspot.hu/2011/03/how-to-change-message-center-number-in.html You may try this as well how to get the smsc number of a phone in android? Apparently there doesn't seem to be a way to programmatically get the number.

Community
  • 1
  • 1
allprog
  • 16,540
  • 9
  • 56
  • 97
  • 1
    If the user has two phone from the same carrier, the smsc Android will be the same, right ? – Guilherme Torres Castro Apr 22 '15 at 16:45
  • 1
    I guess so. I have no device that I could test this on. – allprog Apr 22 '15 at 18:42
  • Some (most?) carriers have two (or more?) SMSC numbers. I guess you can try to set a different SMSC number for each SIM card. I still have to try but currently I do not have a Dual Sim phone for testing. – Matthew Sep 16 '15 at 18:21
  • Whoops, my comment above was for sending an SMS from a specific SIM card in the phone, if the SIM cards have the same carrier. I just realized that wasn't really the question. – Matthew Sep 16 '15 at 18:24
  • the way you suggested to get smsc number is not programmatically possible . is there any way to get it without user have to worry about it ? – Sagar Nayak Apr 08 '16 at 04:30
  • @SagarNayak I haven't tested since then but as the last sentence of the answer points out: there didn't seem to be a programmatic way to do it. – allprog Apr 08 '16 at 11:26
  • sooo sad . we can't do anything much on dual sim android in telephony service . it is onlypossible 5.1 onward . – Sagar Nayak Apr 08 '16 at 11:35
0

Try this code for sim selection, then use SMS sending method to send an SMS!

//above Android API 22
if (Build.VERSION.SDK_INT > 22) {
//for dual sim mobile
SubscriptionManager localSubscriptionManager = SubscriptionManager.from(this);

if (localSubscriptionManager.getActiveSubscriptionInfoCount() > 1) {
 //if there are two sims in dual sim mobile
    List localList = localSubscriptionManager.getActiveSubscriptionInfoList();
    SubscriptionInfo simInfo = (SubscriptionInfo) localList.get(0);
    SubscriptionInfo simInfo1 = (SubscriptionInfo) localList.get(1);

    final String sim1 = simInfo.getDisplayName().toString();
    final String sim2 = simInfo1.getDisplayName().toString();

}else{
 //if there is 1 sim in dual sim mobile
    TelephonyManager tManager = (TelephonyManager) getBaseContext()
            .getSystemService(Context.TELEPHONY_SERVICE);

    String sim1 = tManager.getNetworkOperatorName();

}

}else{
//below android API 22
        TelephonyManager tManager = (TelephonyManager) getBaseContext()
                .getSystemService(Context.TELEPHONY_SERVICE);

        String sim1 = tManager.getNetworkOperatorName();
}
Lakpriya Senevirathna
  • 2,488
  • 2
  • 17
  • 35