【问题标题】:Android Get Country Name with its codeAndroid获取国家名称及其代码
【发布时间】:2013-11-22 21:34:41
【问题描述】:

我想获取国家名称和国家代码。为此,我使用了以下代码:

Country_Language = getApplicationContext().getResources().getConfiguration().locale.getDisplayLanguage();
Country_code= getApplicationContext().getResources().getConfiguration().locale.getCountry(); // get country code

但我总是得到“US”和“United State”作为国家代码和国家名称,而我的国家不是美国。

感谢任何帮助。

【问题讨论】:

    标签: android country country-codes


    【解决方案1】:

    试试这个:

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

    【讨论】:

    • 感谢您的回复...但是假设我使用不带 SIM 卡的标签...那么?
    • 这会给我同样的输出
    • 请浏览该网址中的所有对话。
    【解决方案2】:
    //try this
    **HardCode Local Configure**
    Locale locale = new Locale("en","South Africa");
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getBaseContext().getResources().updateConfiguration(config,
                    getBaseContext().getResources().getDisplayMetrics());
    
    
    String country_Language = getApplicationContext().getResources().getConfiguration().locale.getDisplayLanguage();
    String Country_code= getApplicationContext().getResources().getConfiguration().locale.getCountry();
    
    System.out.println("County Language :"+country_Language);
    System.out.println("County Code :"+Country_code);
    

    【讨论】:

    • 感谢您的回复...但是假设我选择了语言为英语(美国)...但是如果我不是来自美国那么如何...我得到我的国家代码...
    • 你属于哪个国家?
    • 好的,我编辑了我的答案,看到我更改了本地抛出硬编码,因此强制允许您的应用将本地设置为南非,如果您在设备上设置本地,则只需删除硬编码配置本地代码并将语言设置为您的设备或模拟器上的英语(南非)
    • 感谢您的努力,但是.. 当我启动应用程序并且使用我的应用程序的用户不是来自南非的用户时会产生问题..
    【解决方案3】:

    但我总是得到“US”和“United State”作为国家代码和国家名称,而我的国家不是美国。

    感谢任何帮助。

    这是因为您在没有 SIM 设备的情况下运行此代码。要解决这个问题,您应该在 SIM 设备上运行此代码,然后此代码将显示当前国家名称和设备代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-11
      • 1970-01-01
      • 1970-01-01
      • 2016-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多