-->

如何获得国(或它的ISO代码)?(How to get Country (or its ISO co

2019-06-27 15:38发布

什么是获得国家代码的最佳方式? 截至目前,我知道两种方法之一是通过TelephonyManager和另一个由区域设置是找到国家代码的Android对方最好的和独特的方式来获得。

Answer 1:

试试这个,

TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
      String countryCode = tm.getSimCountryIso();


Answer 2:

总有一些关于这个巨大的讨论,我从来不理解为什么开发人员和公司去的复杂的方式。 由用户选择的语言,意味着他/她希望看到总是在他/她的电话的语言。 他们不打算要在不同的语言比其他人或系统应用。

选择是非常简单的:使用区域设置(获取语言选择作为首选的用户 ),或让你的最好小便它们赶走显示他们的语言信息,他们已经表示,他们不希望看到的事情。

要获取国家代码的使用:

Locale.getDefault().getCountry();


Answer 3:

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

这是优于getSimCountryIso因为getSimCountryIso取决于运营商烧SIM上的国家ISO和它也支持CDMA网络。



Answer 4:

您可以使用此代码来获取ISO2: Locale.getDefault().getCountry()

这让ISO3: Locale.getDefault().getISO3Country()

希望这可以帮助



Answer 5:

我的IP地址解决了这个问题。 你可以得到你的电话的IP地址。 如果您在使用Wi-Fi,然后将IP WiFi热点或您的移动IP服务提供服务器的地址,从IP您可以发送到Web服务或东西来跟踪IP地址的国家也有一些消息来源,如果互联网(数据库)提供它提供了国家知识产权这里是例子http://whatismyipaddress.com/ip-lookup 。



Answer 6:

我认为知识产权是最好的方式,因为它会给你的国家,手机是在momment,

如果你这样做的

    Locale.getDefault().getCountry();,

你得到了国家,用户选择的国家,所以你可以选择英国,你可以在西班牙,也许你需要知道哪里是他在momment

例如:假设您的应用程序可以买东西只在英国,用户是来自西班牙,但他对英格兰的假期,他想购买你的产品...如果你使用

    Locale.getDefault().getCountry(); 

该用户将无法购买你的产品,因此IP是我认为最好的方法



Answer 7:

这里是雷托迈耶的优秀文章: http://android-developers.blogspot.com/2011/06/deep-dive-into-location.html

它描述了不同的技术,让Android设备的位置,包括源代码。 接下来,当你的位置,很容易得到国家它-使用可以使用在线Web服务或离线数据库



Answer 8:

我知道这是一个非常古老的问题,但我认为这将有助于一些开发的:

这是很难找到的国家没有GPS和谷歌服务(中国)。 TelephonyManager如果在您的手机没有SIM将无法正常工作。

Locale ,如果在中国的用户设置自己的语言为英语(你会得到该国将是美国或英国)将无法正常工作。

如果您的应用程序需要连接互联网,然后有一个选项。 您可以使用此API称为IP-API 。 请经过那里的文档首先,如果你是刨去使用这个

还有其他的API这样的freegeoip API



Answer 9:

这是一个完整的例子。 尝试从TelephonyManager国家代码(从SIM卡或CDMA设备),如果没有可用的尝试从本地配置得到它。

private static String getDeviceCountryCode(Context context) {
    String countryCode;

    // try to get country code from 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 SIM) query getNetworkCountryIso()
            countryCode = tm.getNetworkCountryIso();
        }

        if (countryCode != null && countryCode.length() == 2)
            return countryCode.toLowerCase();
    }

    // if network country not available (tablets maybe), get country code from 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 3 chars (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请求,像这样的答案 ,或使用精确位置 。

参考这里和这里



文章来源: How to get Country (or its ISO code)?