获取国家代码的最佳方法是什么?
到目前为止,我知道两种方法。一种是通过 TelephonyManager 获取,另一种是通过 Locale 获取。在 Android 上查找国家/地区代码的另一种最佳且独特的方式是什么?
获取国家代码的最佳方法是什么?
到目前为止,我知道两种方法。一种是通过 TelephonyManager 获取,另一种是通过 Locale 获取。在 Android 上查找国家/地区代码的另一种最佳且独特的方式是什么?
尝试这个:
TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String countryCode = tm.getSimCountryIso();
关于这个总是有大量的讨论,我永远不明白为什么开发人员和公司会选择复杂的方式。用户选择的语言,是指他/她希望在他/她的手机中始终看到的语言。他们不打算让应用程序使用与其他人或系统不同的语言。
选择非常直接:使用区域设置(获取用户选择的首选语言),或者尽最大努力让他们以他们已经说过不想看到的语言向他们展示信息。
要获取国家代码,请使用:
Locale.getDefault().getCountry();
利用:
TelephonyManager tm = (TelephonyManager)getSystemService(getApplicationContext().TELEPHONY_SERVICE);
String countryCode = tm.getNetworkCountryIso();
好过getSimCountryIso
因为getSimCountryIso
依赖运营商把国家ISO烧到SIM卡里,而且还支持CDMA网络。
我通过IP地址解决了它。您可以获取手机的 IP 地址。如果您使用的是 Wi-Fi,那么它将是 Wi-Fi热点的IP 地址或移动服务提供商服务器的 IP 地址,因此您可以从该 IP 地址发送到 Web 服务或跟踪该国家/地区的其他东西IP地址。
如果 Internet 提供 IP 地址所在的国家/地区,则有一些资源(数据库)可用。
我认为 IP 地址是最好的方法,因为它会为您提供当前电话所在的国家/地区。
如果你这样做
Locale.getDefault().getCountry();
你得到用户选择国家的国家,所以你可以选择英格兰,你可以在西班牙,也许你需要知道他/她现在在哪里。
示例:假设您的应用只能在英格兰购买商品。用户来自西班牙,但他/她正在英国度假,他/她想购买您的产品……如果您使用
Locale.getDefault().getCountry();
该用户将无法购买您的产品,因此 IP 地址是我认为的最佳方式。
Reto Meier 有一篇很棒的文章: http ://android-developers.blogspot.com/2011/06/deep-dive-into-location.html
它描述了获取 Android 设备位置的不同技术,包括源代码。
没有 GPS 和 Google 服务(中国)的国家很难找到。TelephonyManager
如果您的手机中没有任何 SIM 卡,则无法使用。
如果Locale
中国用户将他/她的语言设置为英语(您将获得的国家/地区将是美国或英国),则将无法使用。
如果您的应用程序需要 Internet 连接,则可以选择。您可以使用这个名为ip-api 的 API。如果您打算使用它,请先阅读他们的文档。
还有其他 API,例如freegeoip API。
这是一个完整的例子。尝试从 TelephonyManager(从 SIM 卡或 CDMA 设备)获取国家代码,如果不可用,请尝试从本地配置中获取。
private static String getDeviceCountryCode(Context context) {
String countryCode;
// Try to get the country code from the TelephonyManager service
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
if(tm != null) {
// Query first getSimCountryIso()
countryCode = tm.getSimCountryIso();
if (countryCode != null && countryCode.length() == 2)
return countryCode.toLowerCase();
if (tm.getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA) {
// Special case for CDMA devices
countryCode = getCDMACountryIso();
} else {
// For 3G devices (with a SIM card), query getNetworkCountryIso()
countryCode = tm.getNetworkCountryIso();
}
if (countryCode != null && countryCode.length() == 2)
return countryCode.toLowerCase();
}
// If the network country is not available (tablets maybe), get the country code from the Locale class
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
countryCode = context.getResources().getConfiguration().getLocales().get(0).getCountry();
} else {
countryCode = context.getResources().getConfiguration().locale.getCountry();
}
if (countryCode != null && countryCode.length() == 2)
return countryCode.toLowerCase();
// General fallback to "us"
return "us";
}
@SuppressLint("PrivateApi")
private static String getCDMACountryIso() {
try {
// Try to get country code from SystemProperties private class
Class<?> systemProperties = Class.forName("android.os.SystemProperties");
Method get = systemProperties.getMethod("get", String.class);
// Get homeOperator that contain MCC + MNC
String homeOperator = ((String) get.invoke(systemProperties,
"ro.cdma.home.operator.numeric"));
// First three characters (MCC) from homeOperator represents the country code
int mcc = Integer.parseInt(homeOperator.substring(0, 3));
// Mapping just countries that actually use CDMA networks
switch (mcc) {
case 330: return "PR";
case 310: return "US";
case 311: return "US";
case 312: return "US";
case 316: return "US";
case 283: return "AM";
case 460: return "CN";
case 455: return "MO";
case 414: return "MM";
case 619: return "SL";
case 450: return "KR";
case 634: return "SD";
case 434: return "UZ";
case 232: return "AT";
case 204: return "NL";
case 262: return "DE";
case 247: return "LV";
case 255: return "UA";
}
}
catch (ClassNotFoundException ignored) {
}
catch (NoSuchMethodException ignored) {
}
catch (IllegalAccessException ignored) {
}
catch (InvocationTargetException ignored) {
}
catch (NullPointerException ignored) {
}
return null;
}