**

文章已过时,通过现有代码可判断

**
背景:
以前google默认的是单卡的,
双卡双待为国内厂商定制,
所以没有直接的系统API来获取正在联网的是哪张卡;
步骤:
主要分三种情况:
1、Android5.0之前的,目前先放弃
2、在Android5.0后可以通过ISub来获取哪张卡在联网,
通过反射获取对应的IMSI
3、Android7.0后通过SubscriptionManager.getDefaultDataSubscriptionId();获取联网卡槽的信息,再根据反射获取对应的IMSI
主要代码如下:

package com.main.common.utils;import android.content.Context;
import android.os.Build;
import android.os.IBinder;
import android.os.RemoteException;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;import com.android.internal.telephony.ISub;
import com.ylmf.androidclient.DiskApplication;import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;/*** @author zhanshuyong* @date 2018/5/23* Description:* 不管是否为双卡双待,可以获取正在联网的那张卡的IMSI* https://blog.csdn.net/ymcl_hx/article/details/53484797* https://blog.csdn.net/linsir007/article/details/51918227* https://blog.csdn.net/u013686019/article/details/71195230* https://blog.csdn.net/u012904691/article/details/78032076* }*/
public class TelephonyManagerUtil {/*** 获取正在联网的那张手机卡  的 imsi值** @return*/public static String getIMSI() {String IMSI = "";int dataSubId = 0;try {Method method;if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {dataSubId = SubscriptionManager.getDefaultDataSubscriptionId();} else {ISub iSub = getISub();if (iSub != null) {dataSubId = getISub().getDefaultDataSubId();}}TelephonyManager telephonyManager = (TelephonyManager) DiskApplication.getInstance().getSystemService(Context.TELEPHONY_SERVICE);Class<?> telephonyClass = telephonyManager.getClass();//如果是5.0的反射long型if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP) {method = telephonyClass.getMethod("getSubscriberId", long.class);} else {//5.0以后用intmethod = telephonyClass.getMethod("getSubscriberId", int.class);}IMSI = method.invoke(telephonyManager, dataSubId).toString();} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | RemoteException e) {e.printStackTrace();}Logger.azhansy("getSubId= " + dataSubId + "  IMSI=" + IMSI);return IMSI;}/*** 5.0以后 android就加了这个** @return*/private static ISub getISub() {try {return ISub.Stub.asInterface((IBinder) Class.forName("android.os.ServiceManager").getMethod("getService", String.class).invoke(null, "isub"));} catch (Exception e) {e.printStackTrace();}return null;}
}

Android5.0后通过iSub.aidl:
注意包名、反射的使用:

/** Copyright (C) 2014 The Android Open Source Project** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.* https://github.com/aosp-mirror/platform_frameworks_base/blob/4b1a8f46d6ec55796bf77fd8921a5a242a219278/telephony/java/com/android/internal/telephony/ISubscriptionListener.aidl*/package com.android.internal.telephony;import android.app.PendingIntent;
import android.telephony.SubscriptionInfo;
import com.android.internal.telephony.ISubscriptionListener;interface ISub {/*** @param callingPackage The package maing the call.* @return a list of all subscriptions in the database, this includes* all subscriptions that have been seen.*/List<SubscriptionInfo> getAllSubInfoList(String callingPackage);/*** @param callingPackage The package maing the call.* @return the count of all subscriptions in the database, this includes* all subscriptions that have been seen.*/int getAllSubInfoCount(String callingPackage);/*** Get the active SubscriptionInfo with the subId key* @param subId The unique SubscriptionInfo key in database* @param callingPackage The package maing the call.* @return SubscriptionInfo, maybe null if its not active*/SubscriptionInfo getActiveSubscriptionInfo(int subId, String callingPackage);/*** Get the active SubscriptionInfo associated with the iccId* @param iccId the IccId of SIM card* @param callingPackage The package maing the call.* @return SubscriptionInfo, maybe null if its not active*/SubscriptionInfo getActiveSubscriptionInfoForIccId(String iccId, String callingPackage);/*** Get the active SubscriptionInfo associated with the slotIdx* @param slotIdx the slot which the subscription is inserted* @param callingPackage The package maing the call.* @return SubscriptionInfo, maybe null if its not active*/SubscriptionInfo getActiveSubscriptionInfoForSimSlotIndex(int slotIdx, String callingPackage);/*** Get the SubscriptionInfo(s) of the active subscriptions. The records will be sorted* by {@link SubscriptionInfo#getSimSlotIndex} then by {@link SubscriptionInfo#getSubscriptionId}.** @param callingPackage The package maing the call.* @return Sorted list of the currently {@link SubscriptionInfo} records available on the device.* <ul>* <li>* If null is returned the current state is unknown but if a {@link OnSubscriptionsChangedListener}* has been registered {@link OnSubscriptionsChangedListener#onSubscriptionsChanged} will be* invoked in the future.* </li>* <li>* If the list is empty then there are no {@link SubscriptionInfo} records currently available.* </li>* <li>* if the list is non-empty the list is sorted by {@link SubscriptionInfo#getSimSlotIndex}* then by {@link SubscriptionInfo#getSubscriptionId}.* </li>* </ul>*/List<SubscriptionInfo> getActiveSubscriptionInfoList(String callingPackage);/*** @param callingPackage The package making the call.* @return the number of active subscriptions*/int getActiveSubInfoCount(String callingPackage);/*** @return the maximum number of subscriptions this device will support at any one time.*/int getActiveSubInfoCountMax();/*** Add a new SubscriptionInfo to subinfo database if needed* @param iccId the IccId of the SIM card* @param slotId the slot which the SIM is inserted* @return the URL of the newly created row or the updated row*/int addSubInfoRecord(String iccId, int slotId);/*** Set SIM icon tint color by simInfo index* @param tint the icon tint color of the SIM* @param subId the unique SubscriptionInfo index in database* @return the number of records updated*/int setIconTint(int tint, int subId);/*** Set display name by simInfo index* @param displayName the display name of SIM card* @param subId the unique SubscriptionInfo index in database* @return the number of records updated*/int setDisplayName(String displayName, int subId);/*** Set Sim Provisioning Status by subscription ID* @param simProvisionStatus with the subscription:* {@See SubscriptionManager#SIM_PROVISIONED}* {@See SubscriptionManager#SIM_UNPROVISIONED_COLD}* {@See SubscriptionManager#SIM_UNPROVISIONED_OUT_OF_CREDIT}* @param subId the unique SubInfoRecord index in database* @return the number of records updated*/int setSimProvisioningStatus(int simProvisioningStatus, int subId);/*** Set display name by simInfo index with name source* @param displayName the display name of SIM card* @param subId the unique SubscriptionInfo index in database* @param nameSource, 0: DEFAULT_SOURCE, 1: SIM_SOURCE, 2: USER_INPUT* @return the number of records updated*/int setDisplayNameUsingSrc(String displayName, int subId, long nameSource);/*** Set phone number by subId* @param number the phone number of the SIM* @param subId the unique SubscriptionInfo index in database* @return the number of records updated*/int setDisplayNumber(String number, int subId);/*** Set data roaming by simInfo index* @param roaming 0:Don't allow data when roaming, 1:Allow data when roaming* @param subId the unique SubscriptionInfo index in database* @return the number of records updated*/int setDataRoaming(int roaming, int subId);int getSlotId(int subId);int[] getSubId(int slotId);int getDefaultSubId();int clearSubInfo();int getPhoneId(int subId);/*** Get the default data subscription* @return Id of the data subscription*/int getDefaultDataSubId();void setDefaultDataSubId(int subId);int getDefaultVoiceSubId();void setDefaultVoiceSubId(int subId);int getDefaultSmsSubId();void setDefaultSmsSubId(int subId);void clearDefaultsForInactiveSubIds();int[] getActiveSubIdList();void setSubscriptionProperty(int subId, String propKey, String propValue);String getSubscriptionProperty(int subId, String propKey, String callingPackage);/*** Get the SIM state for the slot idx* @return SIM state as the ordinal of IccCardConstants.State*/int getSimStateForSlotIdx(int slotIdx);boolean isActiveSubId(int subId);
}
/** Copyright (C) 2014 The Android Open Source Project** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package com.android.internal.telephony;import android.telephony.SubscriptionInfo;oneway interface ISubscriptionListener {void onSubscriptionInfoChanged();
}

参考:
https://blog.csdn.net/ymcl_hx/article/details/53484797
https://blog.csdn.net/linsir007/article/details/51918227
https://blog.csdn.net/u013686019/article/details/71195230
https://blog.csdn.net/u012904691/article/details/78032076

双卡双待获取正在联网的运营商的IMSI相关推荐

  1. Android 获取当前设备SIM运营商

    直接上代码 //获取当前SIM卡运营商名称 public static String getSimOperatorName(Context context) {TelephonyManager tm ...

  2. android获取手机sim卡信息,Android获取手机SIM卡运营商信息的方法

    本文实例讲述了Android获取手机SIM卡运营商信息的方法,对于Android程序设计有非常实用的价值.分享给大家供大家参考之用.具体方法如下: 主要功能代码如下: /** * 获取SIM卡运营商 ...

  3. android 获取sim卡,Android 获取手机SIM卡运营商

    直接上代码: /** * 获取SIM卡运营商 * * @param context * @return */ public static String getOperators(Context con ...

  4. iOS开发 - 获取网关IP,运营商,位置,可判断是在国内还是国外

    你也许会有一个需求,需要判断当前网络的IP地址是在国内还是国外,也许你需要判断连接的Wi-Fi运营商是哪家的,虽然网上也有方法,但是却写得很简单,没有进行深入的说明,所以今天博主重新整理,给大家一个方 ...

  5. Android:获取4G模块卡运营商、网络类型、数据连接状态和信号格数

    代码 import android.annotation.SuppressLint; import android.content.Context; import android.os.Bundle; ...

  6. IOS——获取当前运营商(获取漫游接入网络的运营商)

    这个问题坑了很久了,终于搞定了 -.-||| 如果使用CoreTelephony.framework 框架,获取的仅仅是Sim卡的运营商(而且还不成功!carrier.name属性为Carrier,坑 ...

  7. Android双卡设备 如何正确获取上网卡运营商类型

    关于Android获取运营商的文章已经很多,大部分都是没有深入测试,在双卡设备上存在读取错误信息的问题,这里给出总结. 首先是读取SIM卡权限的问题 注册 <uses-permission an ...

  8. Android运营商名称的获取及显示过程

    常用的获取方式 手机上运营商名称的显示,通常包括状态栏显示,锁屏界面显示以及设置下面部分菜单的显示,最常用的获取方式如下: Sting operatorName = mSubscriptionInfo ...

  9. iOS状态栏操作之获取WiFi信号强度,WiFi名字和IP,网络类型,运营商,电池电量,系统时间等...

    iOS开发中对状态栏的操作比较少,因为状态栏是系统级别的View,是受苹果保护的,不可以随便更改,不可以随便遮挡住.如果违反这些苹果的规范,那么APP将有可能上不了架.笔者就曾写过一个APP,因状态栏 ...

最新文章

  1. 【Python培训基础】一篇文件教你py文件打包成exe
  2. 使用jQuery实现ajax几种方法
  3. 51CTO学院双十一营收400万,预测IT职业教育市场增长150%
  4. CentOS7.1 KVM虚拟化之虚拟机添加磁盘
  5. Codeforces 140D - New Year Contest
  6. python 4舍5入_python中的四舍五入
  7. 《从问题到程序:用Python学编程和计算》——1.2 Python语言简介
  8. Leetcode每日一题:454.4sum-ii(四数相加Ⅱ)
  9. JavaScript 对象总结
  10. micropython入门教程-我的MicroPython入门之路
  11. 【廖雪峰官方网站/Java教程】多线程(1)
  12. VS2008添加ODBC数据源崩溃
  13. tcp/udp/socket 端口映射,转发小工具
  14. 如何修改iTunes的iPhone备份路径?
  15. windows安装配置libtorrent
  16. 了解一下 Technorati 的后台数据库架构
  17. qcap 教程_给winpe添加explorer教程(续):文件列表
  18. Bluetooth Profile Specification之1.2 A2DP 之Audio Codec(音频编解码器)-MPEG-1,2 Audio和MPEG-2, 4 AAC
  19. css制作自动旋转的钟表
  20. 高等数学——导数的定义和常见导数

热门文章

  1. SpringBoot+Vue+虹软(ArcSoft) 的一个在线人脸识别Web系统,可通过调用笔记本摄像头或者网络摄像头实时的进行人脸识别。
  2. ShareSDK 苹果登录实现
  3. java微服务环境配置——注册中心 配置中心Nacos
  4. 3西格玛计算公式_6 西格玛相关的计算
  5. python itertools模块位置_Python之itertools模块
  6. 公司注册流程及税务相关知识整理
  7. 学java还是安卓_零基础学安卓好还是学java好
  8. CGB JAVA面试题 NOTE1
  9. apt 与 dpkg 区别
  10. 了解知识 ,预防意外,有备无患!