语言环境返回没有国家代码的语言环境

时间:2018-09-05 10:23:37

标签: android locale

我需要从默认语言环境中获取国家/地区代码。

ConfigurationCompat.getLocales(context.resources.configuration).get(0)

它返回带有语言的语言环境,但是国家/地区是一个空字符串。

是什么原因?我如何获得国家代码?

3 个答案:

答案 0 :(得分:0)

使用此获取国家/地区

String locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    locale = context.getResources().getConfiguration().getLocales().get(0).getCountry();
} else {
    locale = context.getResources().getConfiguration().locale.getCountry();
}

答案 1 :(得分:0)

  

注意:-在API级别24中已弃用

String locale = context.getResources().getConfiguration().locale.getCountry();

OR

//这将从使用其运营商网络返回国家代码

TelephonyManager tm = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
String countryCodeValue = tm.getNetworkCountryIso();

答案 2 :(得分:0)

使用此代码获取国家/地区代码:

    String locale;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        locale = context.getResources().getConfiguration().getLocales().get(0).getCountry();
    } else {
        locale = context.getResources().getConfiguration().locale.getCountry();
    }
相关问题