Skip to content Skip to sidebar Skip to footer

How To Check Which Sim Is Set As Default Sim In Android Programatically

I am trying to check if my if my mobile device is dual sim, if sim1 is ready, if sim2 is ready, I am done with this using java reflection, now i want to find out if sim1 isRoaming

Solution 1:

You can do something like this:

publicint  getDefaultSimmm(Context context) {

    Object tm = context.getSystemService(Context.TELEPHONY_SERVICE);
    Method method_getDefaultSim;
    int defaultSimm = -1;
    try {
        method_getDefaultSim = tm.getClass().getDeclaredMethod("getDefaultSim");
        method_getDefaultSim.setAccessible(true);
        defaultSimm = (Integer) method_getDefaultSim.invoke(tm);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    Method method_getSmsDefaultSim;
    int smsDefaultSim = -1;
    try {
        method_getSmsDefaultSim = tm.getClass().getDeclaredMethod("getSmsDefaultSim");
        smsDefaultSim = (Integer) method_getSmsDefaultSim.invoke(tm);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return smsDefaultSim;
    }

Solution 2:

Starting from API 22 (Lollipop MR1) android has officially added SubscriptionManager class which gives all the information required by the developer in relation to sim cards and related services.

Documentation for SubscriptionManager

However support for retrieving defaults for calls, SMS and Mobile data were added in API 24. If you use your minimum SDK version to 24 you can use getDefaultSmsSubscriptionId() method to get SMS default set by the user

SubscriptionManagermanager= context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
intdefaultSmsId= manager.getDefaultSmsSubscriptionId();
SubscriptionInfoinfo= manager.getActiveSubscriptionInfo(defaultSmsId);

Note: Above mention call requires READ_PHONE_STATE permission. Make sure you add it in your manifest file

Solution 3:

A very late answer but I got into developing an application which has the above requirement

Below is the latest way to get it done.

/**
     * @return - True - if any sim selected as default sim , False - No default sim is selected or
     * permission for reading the sim status is not enabled
     */booleanisDefaultSimSetForCall() {
        if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            Log.d(Utils.getTag(), "Read Phone state permission Disabled");
            genericCallbacks.onPermissionAccessError(Constants.PermissionErrorCodes.READ_PHONE_STATE_ACCESS);
            returnfalse;
        } else {
            PhoneAccountHandledefaultPhoneAccount= telecomManager.getDefaultOutgoingPhoneAccount(Uri.fromParts("tel", "text", null).getScheme());
            if (defaultPhoneAccount != null) {
                Log.d(Utils.getTag(), "DefaultOutgoingPhoneAccount: " + defaultPhoneAccount.getId());
                returntrue;
            }
        }
        returnfalse;
    }

From the received PhoneAccountHandle, we can get the necessary fields

Post a Comment for "How To Check Which Sim Is Set As Default Sim In Android Programatically"