0

To send sms programmatically I am using,

SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(toNumber, null, text, PendingIntent.getBroadcast(
        this, 0, new Intent(SMS_SENT_ACTION), 0), null);

I have coded for api >=22 like,

SubscriptionManager subscriptionManager=(SubscriptionManager)getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);

        List<SubscriptionInfo> subscriptionInfoList=subscriptionManager.getActiveSubscriptionInfoList();

        if(subscriptionInfoList!=null && subscriptionInfoList.size()>0){
           for(SubscriptionInfo info:subscriptionInfoList){

               String mobileNo=info.getNumber();

           }

        }

using subscription manager I am not getting sim 2 card number using info.getNumber();. It returns empty for Samsung j5 device.

Another important thing is, using SubscriptionManager I am getting line 1 number correctly but empty for line 2. But at the same time when I am trying to send sms using SmsManager.getDefault() it sends sms from line 2. In that device line 2 sim card is set as default.

But based on operators available in a dual sim device toNumber (the number to send sms) will be changed. Because of this I need to know operator name or sim card number of default sim of the device set by user to the number SmsManager.getDefault(). How can I get it know?

Exigente05
  • 2,161
  • 3
  • 22
  • 42
  • Possible duplicate of [How to get carrier name from dual sim phone Android?](http://stackoverflow.com/questions/34714656/how-to-get-carrier-name-from-dual-sim-phone-android) – opt05 Jan 06 '17 at 20:13
  • in my case, I am not getting sim 2 number for marshmallow following first link. That's why I asked this – Exigente05 Jan 06 '17 at 20:14
  • And you are using the SubscriptionManager method as outlined in that question? – opt05 Jan 06 '17 at 20:16
  • updated question. – Exigente05 Jan 06 '17 at 20:18
  • Do the other properties work; info.getCarrierName().toString(), info.getCountryIso() & info.getDataRoaming()? – opt05 Jan 06 '17 at 20:26
  • if yes, how I would be sure which one is default that SmsManager.getDefault() will use? I need to know which line/sim is default set in device. – Exigente05 Jan 06 '17 at 20:37
  • the question you are referring and mine are different. In that I might get both sim cards info but can't know which sim is default but in my question I asked to get default sim info. Default line would also be ok. – Exigente05 Jan 06 '17 at 20:39
  • After a bit of research, the SubscriptionManager only returns a number in getNumber() depending on the stored number in your SIM settings; http://i.imgur.com/GsH04DQ.png. – opt05 Jan 06 '17 at 20:43
  • So, if there is no number stored I won't be able to get it? – Exigente05 Jan 06 '17 at 20:46
  • From my understanding, yes – opt05 Jan 06 '17 at 20:47
  • suppose I got both number and all info, how can I come to know which one is default line/sim set by user? - this is what I asked actually – Exigente05 Jan 06 '17 at 20:48
  • I am assuming in your case that SubscriptionManager.getDefaultSubscriptionId() should get the default sim. But, it sounds like you are trying to get the default SMS, so try SubscriptionManager.getDefaultSmsSubscriptionId() – opt05 Jan 06 '17 at 21:00
  • These two methods are included in api level 24. https://developer.android.com/reference/android/telephony/SubscriptionManager.html . What about < 24 ? – Exigente05 Jan 06 '17 at 21:09
  • can you please remove the flag please as my question is not same as you flagged. – Exigente05 Jan 06 '17 at 21:12
  • Sadly, I am thinking you either have to ask the user or assume it is always the first SIM for API 22 & 23. Google greatly expanded dual SIM in 24 – opt05 Jan 06 '17 at 21:13
  • I assumed, first one would be default, but sim 2 could also be, see my question - I mentioned it. In a big trouble ! Suggest me please that I can come out from this problem. – Exigente05 Jan 06 '17 at 21:17
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/132537/discussion-between-opt05-and-exigente05). – opt05 Jan 06 '17 at 21:24

2 Answers2

1

This is how you should get it (link here):

Get the SmsManager associated with the default subscription id. The instance will always be associated with the default subscription id, even if the default subscription id changes.

Note: For devices that support multiple active subscriptions at a time, SmsManager will track the subscription set by the user as the default SMS subscription. If the user has not set a default, SmsManager may start an activity to kick off a subscription disambiguation dialog. Most operations will not complete until the user has chosen the subscription that will be associated with the operation. If the user cancels the dialog without choosing a subscription, one of the following will happen, depending on the target SDK version of the application. For compatibility purposes, if the target SDK level is <= 28, telephony will still send the SMS over the first available subscription. If the target SDK level is > 28, the operation will fail to complete.

Note: If this method is used to perform an operation on a device that has multiple active subscriptions, the user has not set a default SMS subscription, and the operation is being performed while the application is not in the foreground, the SMS disambiguation dialog will not be shown. The result of the operation will conclude as if the user cancelled the disambiguation dialog and the operation will finish as outlined above, depending on the target SDK version of the calling application. It is safer to use getSmsManagerForSubscriptionId(int) if the application will perform the operation while in the background because this can cause unpredictable results, such as the operation being sent over the wrong subscription or failing completely, depending on the user's default SMS subscription setting.

So, the code would be:

    val subscriptionManager = applicationContext.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE) as SubscriptionManager
    val defaultSubscriptionId = SmsManager.getDefaultSmsSubscriptionId()
    val smsManager = SmsManager.getSmsManagerForSubscriptionId(defaultSubscriptionId)?:SmsManager.getDefault()
    var defaultSubscriptionInfo: SubscriptionInfo? = subscriptionManager.getActiveSubscriptionInfo(defaultSubscriptionId)
android developer
  • 114,585
  • 152
  • 739
  • 1,270
0

To sum everything up, it seems there may be a bug in the Samsung J5 implementation of the dual SIM on Android 23 (resulting in a blank phone number in the SIM settings).

opt05
  • 813
  • 11
  • 21