How to get default sim card number android programmatically

3.5k views Asked by At

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?

2

There are 2 answers

0
opt05 On

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).

0
android developer On

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)