Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 and if sim2 isRoaming, and if its dual sim which sim is set as default. Is it possible with the help of java reflection.

like image 861
Developine Avatar asked Aug 19 '15 07:08

Developine


2 Answers

You can do something like this:

public int  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;
    }
like image 195
IB's Avatar answered Sep 28 '22 17:09

IB's


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

SubscriptionManager manager = context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
int defaultSmsId = manager.getDefaultSmsSubscriptionId();
SubscriptionInfo info = manager.getActiveSubscriptionInfo(defaultSmsId);

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

like image 37
Apurva Avatar answered Sep 28 '22 18:09

Apurva