[轉]android 判斷國家是否是國內使用者(不用經緯度)
阿新 • • 發佈:2018-11-20
/**
* 判斷國家是否是國內使用者
*
*方法一
*
* @return
*/
public static boolean isCN(Context context) {
TelephonyManager tm = (TelephonyManager) context.getSystemService(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) c.getSystemService(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");
}
}