最新消息:20210917 已从crifan.com换到crifan.org

【无法解决】安卓10的小米9中通过代码获取到IMSI和MSIN

代码 crifan 749浏览 0评论
折腾:
【未解决】安卓10的小米9中中国移动手机号的SIM的MSIN是什么
期间,通过adb命令获取不到IMSI
参考帖子去试试代码获取:
另外看到:
TelephonyManager.getSimOperator(): 46009  // 当前流量卡,联通
TelephonyManager.getSimOperatorName(): CMCC  // 联通
TelephonyManager.getNetworkOperator(): 46000  // 移动,卡一
TelephonyManager.getNetworkOperatorName(): CHINA MOBILE  // 移动
TelephonyManager.getSubscriberId(): 46002326951xxxx  // 移动,这就是 IMSI,xxxx 部分是由我隐去的
Operator name: 联通
别人用
TelephonyManager.getSubscriberId()
可以获取到对应的
46002326951xxxx
就是:
IMSI
其中最后10位就是:MSIN
Android获取手机运营商、有无sim卡 – 简书
抽空去试试:
把相关代码
TelephonyManager.getSubscriberId()
加到此处rcsjta中,看看能否直接获取到 小米9的IMSI
android IMSI 获取
Android中获取手机IMEI,IMSI, MAC(Android 6.0、支持4G环境获取)工具类(标识用户唯一)_静心 Study-CSDN博客
    /**
     * 获取手机IMSI
     */
    public static String getIMSI(Context context){
        try {
            TelephonyManager telephonyManager=(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
            //获取IMSI号
            String imsi=telephonyManager.getSubscriberId();
            if(null==imsi){
                imsi="";
            }
            return imsi;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }
和之前代码一样
Android 获取 IMSI、IMEI_尘缘驿站-CSDN博客
/**
 * 反射获取 getSubscriberId ,既imsi
 *
 * @param subId
 * @return
 */
 public static String getSubscriberId(int subId) {
     TelephonyManager telephonyManager = (TelephonyManager) App.getInstance().getSystemService(TELEPHONY_SERVICE);// 取得相关系统服务
     Class<?> telephonyManagerClass = null;
     String imsi = null;
     try {
         telephonyManagerClass = Class.forName("android.telephony.TelephonyManager");


         if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.LOLLIPOP) {
             Method method = telephonyManagerClass.getMethod("getSubscriberId", int.class);
             imsi = (String) method.invoke(telephonyManager, subId);
         } else if (android.os.Build.VERSION.SDK_INT == android.os.Build.VERSION_CODES.LOLLIPOP) {
             Method method = telephonyManagerClass.getMethod("getSubscriberId", long.class);
             imsi = (String) method.invoke(telephonyManager, (long) subId);
         }
     } catch (Exception e) {
         e.printStackTrace();
     }


     Log.i(App.TAG, "IMSI==" + imsi);
     return imsi;
 }


 /**
  * 反射获取 getSubscriptionId ,既 subid
  *
  * @param slotId 卡槽位置(0,1)
  * @return
  */
 public static int getSubscriptionId(int slotId) {
     try {
         Method datamethod;
         int setsubid = -1;//定义要设置为默认数据网络的subid
         //获取默认数据网络subid   getDefaultDataSubId
         Class<?> SubscriptionManager = Class.forName("android.telephony.SubscriptionManager");
         if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) { // >= 24  7.0
             datamethod = SubscriptionManager.getDeclaredMethod("getDefaultDataSubscriptionId"); 
         } else {
             datamethod = SubscriptionManager.getDeclaredMethod("getDefaultDataSubId");  
         }
         datamethod.setAccessible(true);
         int SubId = (int) datamethod.invoke(SubscriptionManager);


         Method subManagermethod = SubscriptionManager.getDeclaredMethod("from", Context.class);
         subManagermethod.setAccessible(true);
         Object subManager = subManagermethod.invoke(SubscriptionManager, App.getInstance());


         //getActiveSubscriptionInfoForSimSlotIndex  //获取卡槽0或者卡槽1  可用的subid
         Method getActivemethod = SubscriptionManager.getDeclaredMethod("getActiveSubscriptionInfoForSimSlotIndex", int.class);
         getActivemethod.setAccessible(true);
         Object msubInfo = getActivemethod.invoke(subManager, slotId);  //getSubscriptionId


         Class<?> SubInfo = Class.forName("android.telephony.SubscriptionInfo");


         //slot0   获取卡槽0的subid
         int subid = -1;
         if (msubInfo != null) {
             Method getSubId0 = SubInfo.getMethod("getSubscriptionId");
             getSubId0.setAccessible(true);
             subid = (int) getSubId0.invoke(msubInfo);
         }
         Log.i(App.TAG, "slotId=" + slotId + ", subid=" + subid);
         return subid;
     } catch (Exception e) {
         Log.e(App.TAG, e.getLocalizedMessage());
     }
     return -1;
 }


 /**
  * 获取运营商 IMSI
  * 默认为 IMEI1对应的 IMSI
  *
  * @return
  */
 public static String getSimOperator() {
     TelephonyManager telephonyManager = (TelephonyManager) App.getInstance().getSystemService(TELEPHONY_SERVICE);// 取得相关系统服务
     return telephonyManager.getSimOperator();
 }
Android 获取双卡手机IMEI,IMSI,ICCID – 简书
TelephonyManager telephonyManager = (TelephonyManager) this
                .getSystemService(TELEPHONY_SERVICE);// 取得相关系统服务

        String imsi = telephonyManager.getSubscriberId();     //取出 IMSI
Android中获取手机IMEI,IMSI, MAC(Android 6.0) – 云+社区 – 腾讯云
算了,去加代码试试
写了代码:
import android.telephony.TelephonyManager;

TelephonyManager telephonyManager = (TelephonyManager) App.getInstance().getSystemService(TELEPHONY_SERVICE);// 取得相关系统服务
String imsi = telephonyManager.getSubscriberId();
sLogger.debug("IMSI=".concat(imsi));
有错误提示:
另外还发现
app红色报错
参考
Android获取手机运营商、有无sim卡 – 简书
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
去试了换context
TelephonyManager telephonyManager = (TelephonyManager) context.getInstance().getSystemService(TELEPHONY_SERVICE);// 取得相关系统服务
问题依旧
参考:
Android 获取双卡手机IMEI,IMSI,ICCID – 简书
换this试试
TelephonyManager telephonyManager = (TelephonyManager) this
        .getSystemService(TELEPHONY_SERVICE);// 取得相关系统服务
貌似不报错了。
再继续解决后面的错误
进入getSubscriberId的定义
/Users/xxx/Library/Android/sdk/sources/android-26/android/telephony/TelephonyManager.java
/**
 * Returns the unique subscriber ID, for example, the IMSI for a GSM phone.
 * Return null if it is unavailable.
 */
@RequiresPermission(android.Manifest.permission.READ_PHONE_STATE)
public String getSubscriberId() {
    return getSubscriberId(getSubId());
}
可以看到,需要添加权限
所以去加上:
/RCS/rcsjta/core/AndroidManifest.xml
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
发现之前已有此权限了
不用额外添加了。
试试 add permissions check
自动加了判断代码:
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
    // TODO: Consider calling
    //    ActivityCompat#requestPermissions
    // here to request the missing permissions, and then overriding
    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
    //                                          int[] grantResults)
    // to handle the case where the user grants the permission. See the documentation
    // for ActivityCompat#requestPermissions for more details.
    return;
}
String imsi = telephonyManager.getSubscriberId();
此处代码是:
//        TelephonyManager telephonyManager = (TelephonyManager) App.getInstance().getSystemService(TELEPHONY_SERVICE);// 取得相关系统服务
//        TelephonyManager telephonyManager = (TelephonyManager) context.getInstance().getSystemService(TELEPHONY_SERVICE);// 取得相关系统服务
        TelephonyManager telephonyManager = (TelephonyManager) this
                .getSystemService(TELEPHONY_SERVICE);// 取得相关系统服务
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        String imsi = telephonyManager.getSubscriberId();
        sLogger.debug("IMSI=".concat(imsi));
去调试看看:
能否获取到
不过另外看到了
稳定获取Android设备唯一码(UUID)的解决方案-阿里云开发者社区
”Android中设备唯一码有很多,如:MAC地址、IMEI号(DeviceId)、IMSI号、ANDROID_ID、序列号(SerialNumber)等,
但并不是所有设备上都能稳定获取到这些值。“
android 代码获取IMEI,IMSI都为空,怎么解决?_百度知道
”权限也都加了,获取还是空。
手机号码不是所有的都能获取。只是有一部分可以拿到。这个是由于移动运营商没有把手
机号码的数据写入到sim卡中。sim卡只有唯一的编号,供网络与设备识别那就是imsi号码,手机的信号也可以说是通过这个号码在网络中传递的,并不是手机号码。
试想,你的sim丢失后,补办一张新的会换号码吗? —不会就是因为在你的手机号码对应的 imsi号在移动运营商中被修改成新sim卡的imsi号码。
这个就像是一个变量,当移动运营商为它赋值了,它自然就会有值。不赋值自然为空。“
-》那估计是:
之前adb命令都没获取到
-》就属于:正常现象了
估计此处小米9就没开放这些属性?
那估计代码中也获取不到了。。。
调试发现没权限
那去卸载再重新安装
然后全部允许,或许就有权限了?
重新调试,发现此处代码,在启动之前就允许了
导致还没去弹框允许权限呢。
暂时注释掉
重新运行,确保获取到所有权限
再去加上这段代码来调试看看
不过竟然没弹框申请权限
那么手动去设置添加权限
再调试看看
至少有权限了:
结果:
IMSI依旧是null空
->后记:
还看到了警告提示,可以会是null,抛出null的异常
Using getSubscriberId to get device identifiers is not recommended. 
Method invocation 'getSubscriberId' may produce 'NullPointerException' 
再去试试:
String simOp = telephonyManager.getSimOperator();
sLogger.debug("Sim Operator=".concat(simOp));
以及:
//        String imei = telephonyManager.getImei();
        String imei = telephonyManager.getDeviceId();
        sLogger.debug("IMEI=".concat(imei));
->也看到警告:
Using getDeviceId to get device identifiers is not recommended.
先去运行试试
是可以获取到sim的operator是46002的
而getDeviceId返回的IMEI是null
【总结】
此处通过代码:
import android.telephony.TelephonyManager;


//        TelephonyManager telephonyManager = (TelephonyManager) App.getInstance().getSystemService(TELEPHONY_SERVICE);// 取得相关系统服务
//        TelephonyManager telephonyManager = (TelephonyManager) context.getInstance().getSystemService(TELEPHONY_SERVICE);// 取得相关系统服务
        TelephonyManager telephonyManager = (TelephonyManager) this
                .getSystemService(TELEPHONY_SERVICE);// 取得相关系统服务
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        String imsi = telephonyManager.getSubscriberId();
        if (imsi != null) {
            sLogger.debug("IMSI=".concat(imsi));
        }
        String simOp = telephonyManager.getSimOperator();
        sLogger.debug("Sim Operator=".concat(simOp));
//        String imei = telephonyManager.getImei();
        String imei = telephonyManager.getDeviceId();
        if (imei != null) {
            sLogger.debug("IMEI=".concat(imei));
        }
结果:
  • IMSI:获取不到,是空null
  • IMEI:获取不到,是空null
  • SIM的operator:可以获取到,是46002
    • 中国移动CMCC
      • 参见

转载请注明:在路上 » 【无法解决】安卓10的小米9中通过代码获取到IMSI和MSIN

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
90 queries in 0.184 seconds, using 23.35MB memory