转载:android 判断手机是否是国内的手机的方法(getSimCountryIso和getSimOperator)
http://blog.csdn.net/mingli198611/article/details/40681071
有一个需求:如果手机插入了sim卡,要判断该手机是否是大陆用户?
方法一:
/**
* 判断国家是否是国内用户*
* @return
*/
public static boolean isCN(Context context) {
TelephonyManager tm = (TelephonyManager) ContextHelper.getSystemService(context, Context.TELEPHONY_SERVICE);
String countryIso = tm.getSimCountryIso();
boolean isCN = false;//判断是不是大陆
if (!TextUtils.isEmpty(countryIso)) {
countryIso = countryIso.toUpperCase(Locale.US);
if (countryIso.contains("CN")) {
isCN = true;
}
}
return isCN;
}
方法二:
/** 查询手机的 MCC+MNC */
private static String getSimOperator(Context c) {
TelephonyManager tm = (TelephonyManager) ContextHelper.getSystemService(c, Context.TELEPHONY_SERVICE);
try {
return tm.getSimOperator();
} catch (Exception e) {
}
return null;
}
/** 因为发现像华为Y300,联想双卡的手机,会返回 "null" "null,null" 的字符串 */
private static boolean isOperatorEmpty(String operator) {
if (operator == null) {
return true;
}
if (operator.equals("") || operator.toLowerCase(Locale.US).contains("null")) {
return true;
}
return false;
}
/** 判断是否是国内的 SIM 卡,优先判断注册时的mcc */
public static boolean isChinaSimCard(Context c) {
String mcc = getSimOperator(c);
if (isOperatorEmpty(mcc)) {
return false;
} else {
return mcc.startsWith("460");
}
}
综上所述:发现方法二更加准确,方法一在部分手机上,比如一些联想国行的手机会出现没有插入sim卡,也能够读取到国家代码为cn。