Detect sim card availability in the Android dual sim device

Published by Igor Khrupin on

Below you can find code which help you find if SIM card available in the device.
There two solutions. One for Android API less than 22 and for Android API greater than or equal to 22.

Problem that dualsim support added in API-22. And we should deal with devices which support dual sim and has API less than 22.

So solution for API less than 22.

public boolean isSimAvailable(Context context){
    TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    if (telephonyManager.getSimSerialNumber() != null){
        return true;
    }
    return false;
}

You can ask why I’m not checking for phone number. I do this because the phone number is not 100% available. Sim card serial number always available.

Second solution for API greater than or equal to 22

public boolean isSimAvailable(Context context){
    SubscriptionManager sManager = (SubscriptionManager) context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
    SubscriptionInfo infoSim1 = sManager.getActiveSubscriptionInfoForSimSlotIndex(0);
    SubscriptionInfo infoSim2 = sManager.getActiveSubscriptionInfoForSimSlotIndex(1);
    if(infoSim1 != null || infoSim2 != null) {
         return true;
    }
    return false;
}

And here is universal solution:

public boolean isSimAvailable(Context context){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
        SubscriptionManager sManager = (SubscriptionManager) context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
        SubscriptionInfo infoSim1 = sManager.getActiveSubscriptionInfoForSimSlotIndex(0);
        SubscriptionInfo infoSim2 = sManager.getActiveSubscriptionInfoForSimSlotIndex(1);
        if(infoSim1 != null || infoSim2 != null) {
             return true;
        }
    }else{
        TelephonyManager telephonyManager = (TelephonyManager) App.getInstance().getSystemService(Context.TELEPHONY_SERVICE);
        if (telephonyManager.getSimSerialNumber() != null){
            return true;
        }
    }
    return false;
}

Let me know your solution in comments.


3 Comments

shekhar · 22 August, 2017 at 07:59

Thanks man,
How can i get both sim details for below version 22.

    Igor Khrupin · 22 August, 2017 at 09:45

    Hi Shekhar.
    I’d the same problem.
    You can’t get sim details but you can get if sim available in phone.
    Phone number detection not working stable for me.
    The best solution is asking user about phone number. This may always works.

    Damu · 16 July, 2018 at 06:55

    Hope there is no dual sim phones were introduced before version 22

    Reference: https://developer.android.com/about/versions/android-5.1

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.