Android 6.x以上取多卡手机的imei imsi

来源:赵克立博客 分类: Android 标签:--发布时间:2018-04-11 10:35:22最后更新:2018-04-11 10:35:22浏览:1548
版权声明:
本文为博主原创文章,转载请声明原文链接...谢谢。o_0。
更新时间:
2018-04-11 10:35:22
温馨提示:
学无止境,技术类文章有它的时效性,请留意文章更新时间,如发现内容有误请留言指出,防止别人"踩坑",我会及时更新文章

加上权限

<!-- 获取硬件信息权限   -->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />


写上两个静态方法

public static Class[] getMethodParamTypes(String methodName) {
    Class[] params = null;
    try {
        Method[] methods = TelephonyManager.class.getDeclaredMethods();
        for (int i = 0; i < methods.length; i++) {
            if (methodName.equals(methods[i].getName())) {
                params = methods[i].getParameterTypes();
                if (params.length >= 1) {
                    LogUtil.d("length:", "" + params.length);
                    break;
                }
            }
        }
    } catch (Exception e) {
        LogUtil.d("", e.toString());
    }
    return params;
}
public static Object getPhoneInfo(int subId, String methodName, Context context) {
    Object value = null;
    try {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (Build.VERSION.SDK_INT >= 21) {
            Method method = tm.getClass().getMethod(methodName, getMethodParamTypes(methodName));
            if (subId >= 0) {
                value = method.invoke(tm, subId);
            }
        }
    } catch (Exception e) {
        LogUtil.d("", e.toString());
    }
    return value;
}

使用方法

//取卡1 imei,卡2是 1
String IMEI=(String) getPhoneInfo(0,"getDeviceId", this);
//取卡1的imsi
String imsi = (String) getPhoneInfo(0, "getSubscriberId", this);



微信号:kelicom QQ群:215861553 紧急求助须知
Win32/PHP/JS/Android/Python