因为某些SIM/USIM卡没有写入卡号,所以通过相关接口读取手机号的时候可能为空,鉴于此,使用IMSI的MCC/MNC来判断运营商是比较准确的选择:
public static boolean isChinaMobile(Context context) { TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); try{ String IMSI = telephonyManager.getSubscriberId(); // IMSI号前面3位460是国家,紧接着后面2位00 02 07是中国移动,01是中国联通,03是中国电信。 if (IMSI.startsWith("46000") || IMSI.startsWith("46002") || IMSI.startsWith("46007")) { return true; } else if (IMSI.startsWith("46001")) { return false; } else if (IMSI.startsWith("46003")) { return false; } }catch(Exception e){ e.printStackTrace(); } return false; }
国内MCC是460,MNC三大运营商各自不同,目前00 02 07属于移动,01属于联通,03属于电信。随着业务的发展,可能有增加,需要注意。