当手机里面插入两张sim卡的时候获取两张imsi码
智能机时代,国内Android手机基本找不到单卡的手机了,android系统api给我们预留了一些默认的方法来获取默认sim卡的方法比如:
当我们要同时获取指定的sim的imsi码的时候,就要花费点时间研究一下了:
我这给出一种通过反射获取imsi码方案:
public static String getIMSI(Context context) { TelephonyManager mTelephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); String imsi = mTelephonyMgr.getSubscriberId(); return imsi; }
当我们要同时获取指定的sim的imsi码的时候,就要花费点时间研究一下了:
我这给出一种通过反射获取imsi码方案:
/* * *获取指定sim卡的IMSI序列号 */ public static String getSimIMSI(TelephonyManager telephonyManager, int simid) { int[] subId = null;//SubscriptionManager.getSubId(simid); Class<?> threadClazz = SubscriptionManager.class; try { Method method = threadClazz.getDeclaredMethod("getSubId", int.class); method.setAccessible(true); subId = (int[]) method.invoke(null, simid); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } Log.d(TAG, "getSimIMSI, simId = " + simid + " subId = " + ((subId != null) ? subId[0] : "invalid!")); int sub = -1; if (Build.VERSION.SDK_INT >= 24) { sub = (subId != null) ? subId[0] : SubscriptionManager.getDefaultSubscriptionId(); } else { if (threadClazz != null) { try { Method method = threadClazz.getDeclaredMethod("getDefaultSubId"); method.setAccessible(true); sub = (subId != null) ? subId[0] : (Integer) method.invoke(null, null); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } } String IMSI = null; if (sub != -1) { Class clazz = telephonyManager.getClass(); try { Method method = clazz.getDeclaredMethod("getSubscriberId",int.class); method.setAccessible(true); IMSI = (String)method.invoke(telephonyManager,sub); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } Log.d(TAG, "IMSI = " + IMSI); if (!TextUtils.isEmpty(IMSI)) { return IMSI; } return null; }