54

获取国家代码的最佳方法是什么?

到目前为止,我知道两种方法。一种是通过 TelephonyManager 获取,另一种是通过 Locale 获取。在 Android 上查找国家/地区代码的另一种最佳且独特的方式是什么?

4

9 回答 9

82

尝试这个:

TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String countryCode = tm.getSimCountryIso();
于 2012-09-06T07:12:02.930 回答
35

关于这个总是有大量的讨论,我永远不明白为什么开发人员和公司会选择复杂的方式。用户选择的语言,是指他/她希望在他/她的手机中始终看到的语言。他们不打算让应用程序使用与其他人或系统不同的语言。

选择非常直接:使用区域设置(获取用户选择的首选语言),或者尽最大努力让他们以他们已经说过不想看到的语言向他们展示信息。

要获取国家代码,请使用:

Locale.getDefault().getCountry();
于 2012-09-09T16:37:09.833 回答
14

利用:

TelephonyManager tm = (TelephonyManager)getSystemService(getApplicationContext().TELEPHONY_SERVICE);
String countryCode = tm.getNetworkCountryIso();

好过getSimCountryIso因为getSimCountryIso依赖运营商把国家ISO烧到SIM卡里,而且还支持CDMA网络。

于 2013-12-23T07:04:56.557 回答
10

您可以使用此代码获取ISO 2

Locale.getDefault().getCountry()

这是为了获得ISO 3

Locale.getDefault().getISO3Country()
于 2016-05-27T01:29:58.317 回答
6

我通过IP地址解决了它。您可以获取手机的 IP 地址。如果您使用的是 Wi-Fi,那么它将是 Wi-Fi热点的IP 地址或移动服务提供商服务器的 IP 地址,因此您可以从该 IP 地址发送到 Web 服务或跟踪该国家/地区的其他东西IP地址。

如果 Internet 提供 IP 地址所在的国家/地区,则有一些资源(数据库)可用。

这是示例http://whatismyipaddress.com/ip-lookup

于 2012-09-07T09:40:01.463 回答
6

我认为 IP 地址是最好的方法,因为它会为您提供当前电话所在的国家/地区。

如果你这样做

Locale.getDefault().getCountry();

你得到用户选择国家的国家,所以你可以选择英格兰,你可以在西班牙,也许你需要知道他/她现在在哪里。

示例:假设您的应用只能在英格兰购买商品。用户来自西班牙,但他/她正在英国度假,他/她想购买您的产品……如果您使用

Locale.getDefault().getCountry();

该用户将无法购买您的产品,因此 IP 地址是我认为的最佳方式。

于 2013-05-10T12:48:36.470 回答
4

Reto Meier 有一篇很棒的文章: http ://android-developers.blogspot.com/2011/06/deep-dive-into-location.html

它描述了获取 Android 设备位置的不同技术,包括源代码。

接下来,当您获得位置时,很容易为它获取国家 - 您可以使用在线网络服务离线数据库

于 2012-09-04T08:39:24.310 回答
2

没有 GPS 和 Google 服务(中国)的国家很难找到。TelephonyManager如果您的手机中没有任何 SIM 卡,则无法使用。

如果Locale中国用户将他/她的语言设置为英语(您将获得的国家/地区将是美国或英国),则将无法使用。

如果您的应用程序需要 Internet 连接,则可以选择。您可以使用这个名为ip-api 的 API。如果您打算使用它,请先阅读他们的文档。

还有其他 API,例如freegeoip API

于 2017-09-13T06:46:20.707 回答
-1

这是一个完整的例子。尝试从 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;
}

另一个想法是尝试像这个答案中的 API 请求,或者使用良好的位置

参考资料在这里这里

于 2018-09-14T08:04:48.840 回答