说实话,要Android手机区分国内还是国外的方法有很多种,我现在就说几种大家都耳熟能详的方法,比如说,根据地图的经纬度来区分国内外,还有根据手机上的语言来区分国内外(这个区分方式是有一点牵强的,因为在中国的,也可以切换输入法,那我怎样判断出来呢)等等,现在我们说一下,第二种区分方式,直接上代码:
/** * 判断国家是否是国内用户 * *方法一 * * @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"); } }