最近业务上需要获取用户SIM运营商来做相关处理,可以通过以下方案直接获取
//获取当前SIM卡运营商名称
//return 与SIM卡有关,如果无卡则返回空
//中国联通或China Unicom
//中国电信或China Telecom
//中国移动或China Mobile
public static String getSimOperatorName(Context context) {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
return tm.getSimOperatorName();
}
//获取当前SIM卡的MCC(移动国家码,前3位数)和MNC(移动网络码,后两位数) 加起来共5位
//MCC码:
//中国香港:454(454, "Hong Kong", "HK", "香港")
//中国澳门:455
//中国大陆:460(460, "China", "CN", "中国")
//中国台湾:466(466, "Taiwan", "TW", "台湾")
//MNC码:
//中国移动:00,02,04,07,08,20,99
//中国联通:01,06,09
//中国电信:03,05,11,12,13
public static String getSimOperator(Context context) {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
return tm.getSimOperator();
}
如果通过getsubscriberId()获取IMSI(国际移动用户识别码)则需要READ_PHONE_STATE权限,如果Android10或以上则要获取READ_PRIVILEGED_PHONE_STATE权限,比较麻烦,如果仅需要获取运营商则可以使用上面方法即可。
API22以后SDK支持了双卡双待,可以通过下面方法获取,但是需要android.permission.READ_PHONE_STATE权限
SubscriptionManager subManager = (SubscriptionManager) context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
if (subManager.getActiveSubscriptionInfoCount() >= 1) {
int mcc1 = subManager.getActiveSubscriptionInfo(0).getMcc();
int mnc1 = subManager.getActiveSubscriptionInfo(0).getMnc();
}
if(subManager.getActiveSubscriptionInfoCount() >= 2) {
int mcc2 = subManager.getActiveSubscriptionInfo(1).getMcc();
int mnc2 = subManager.getActiveSubscriptionInfo(1).getMnc();
}
评论区