Nayan Kurude
Nayan Kurude

Reputation: 183

How to get IMSI No of Second Sim in Android Phone

I was using TelephonyManager and I could get the SubscriberID (IMSI) of SIM1 but couldn't get the same for SIM2. However I was able to get IMEI no for both SIM Slots, but still I am not able to get IMSI for second sim. Is there any way to get this info?

Upvotes: 3

Views: 2722

Answers (2)

Nishant Pardamwar
Nishant Pardamwar

Reputation: 1485

REMEMBER THIS IS HACKY WAY TO GET SUBSCRIBER ID, SO MAY BE IT WONT WORK ON ALL DEVICES

will work on Android 5.1.1 and above and required android.permission.READ_PHONE_STATE permission

(remember to ask user about android.permission.READ_PHONE_STATE permission in above marshmallow android versions as it is marked as dangerous)

public String getSim1IMSI() {
    String imsi = null;
    TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
    try {
        Method getSubId = TelephonyManager.class.getMethod("getSubscriberId", int.class);
        SubscriptionManager sm = (SubscriptionManager) getSystemService(TELEPHONY_SUBSCRIPTION_SERVICE);
        imsi = (String) getSubId.invoke(tm, sm.getActiveSubscriptionInfoForSimSlotIndex(0).getSubscriptionId()); // Sim slot 1 IMSI
        return imsi;
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    }
    return imsi;
}


public String getSim2IMSI() {
    String imsi = null;
    TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
    try {
        Method getSubId = TelephonyManager.class.getMethod("getSubscriberId", int.class);
        SubscriptionManager sm = (SubscriptionManager) getSystemService(TELEPHONY_SUBSCRIPTION_SERVICE);
        imsi = (String) getSubId.invoke(tm, sm.getActiveSubscriptionInfoForSimSlotIndex(1).getSubscriptionId()); // Sim slot 2 IMSI
        return imsi;
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    }
    return imsi;
}

Upvotes: 4

Paxana Non Grata
Paxana Non Grata

Reputation: 389

You can use the getDeviceId method from the telephony manager

https://developer.android.com/reference/android/telephony/TelephonyManager.html#getDeviceId(int)

Upvotes: 0

Related Questions