SubscriptionManager to read IMSI for dual SIM devices ruuning Android 5.1+

那年仲夏 提交于 2020-07-19 05:51:07

问题


For API 22+ I am trying to use SubscriptionManager to read dual SIM IMSI.

The IMSI is a 14 to 15 characters in the following format: "MCC-MNC-MSIN" MCC = Mobile Country Code (e.g. 310 for USA); MNC = Mobile Network Code (e.g. 410 for AT&T), MSIN = sequential serial number.

There is no method in the class to get the IMSI. There are methods to get both MCC & MNC but not MSIN.

So my question is, how to get the full IMSI using SubscriptionManager?

Update: SubscriptionManager returns wrong/same MCC & MNC for different SIMs. Testing on Motorola Moto E running Android 5.1

Update: SubscriptionManager is working fine on Moto G running Android 6.0. But there is no way to read the SIM status.


回答1:


There is a public but hidden method getting the subscriber ID(the IMSI for a GSM phone) for a given subscription ID. I don't know why it is hidden but you can call it without problem using java reflection.

Even getSubscriberId internally calls getSubscriberId(int subId), so i think it is safe to use.

Here's an example.

TelephonyManager telephonyManager = (TelephonyManager) mContext
            .getSystemService(Context.TELEPHONY_SERVICE);
int slotIndex = 1;
int subscriptionId = SubscriptionManager.from(mContext).getActiveSubscriptionInfoForSimSlotIndex(slotIndex).getSubscriptionId();
try {
    Class c = Class.forName("android.telephony.TelephonyManager");
    Method m = c.getMethod("getSubscriberId", new Class[] {int.class});
    Object o = m.invoke(telephonyManager, new Object[]{subscriptionId});

    String subscriberId = (String) o;
} catch (Exception e) {
    e.printStackTrace();
}


来源:https://stackoverflow.com/questions/36902916/subscriptionmanager-to-read-imsi-for-dual-sim-devices-ruuning-android-5-1

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!