Android获取当前的Locale,而不是默认

CQM*_*CQM 275 android locale

如何在Android中获取用户当前的区域设置?

我可以得到默认值,但这可能不是当前的正确吗?

基本上我想要当前语言环境中的双字母语言代码.不是默认的.没有Locale.current()

Dev*_*red 466

默认值Locale是在运行时为系统属性设置中的应用程序进程静态构造的,因此它将表示启动应用程序时该Locale设备上的选定内容.通常,这很好,但它确实意味着如果用户在应用程序进程运行后更改其设置,则可能不会立即更新值.LocalegetDefaultLocale()

如果由于某种原因需要在应用程序中捕获此类事件,则可以尝试Locale从资源Configuration对象中获取可用的事件,即

Locale current = getResources().getConfiguration().locale;
Run Code Online (Sandbox Code Playgroud)

如果您的应用程序需要更改设置,您可能会发现更快地更新此值.

  • 这已在Configuration类中弃用,请参阅此建议的最新文档:`locale此字段在API级别24中已弃用.请勿直接设置或读取此字段.使用getLocales()和setLocales(LocaleList).如果只需要主要语言环境,则getLocales().get(0)现在是首选的访问者 (53认同)
  • 我测试了它,在我不断改变语言环境的同时输出了两个; 结果是,输出是一样的.所以Locale.getDefault()可以安全地使用,因为它会立即更新.`Log.d("localeChange","Default locale lang:"+ Locale.getDefault().getLanguage()); Log.d("localeChange","Config locale lang:"+ getResources().getConfiguration().locale.getLanguage());` (9认同)
  • 为什么他们不只是添加一个getFirstLocale()方法来避免奇怪的getLocales().get(0)? (7认同)
  • 如果你想支持旧版本和新版本的 api 怎么办? (2认同)

Mak*_*ele 160

Android N(Api等级24)更新(无警告):

   Locale getCurrentLocale(Context context){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
            return context.getResources().getConfiguration().getLocales().get(0);
        } else{
            //noinspection deprecation
            return context.getResources().getConfiguration().locale;
        }
    }
Run Code Online (Sandbox Code Playgroud)

  • @FedericoPonzi @ for3st我在`ConfigurationCompat`中找到了这个,作为`ConfigurationCompat.getLocales(getResources().getConfiguration()).get(0)` (16认同)
  • 如果包含if和fallback代码,则代码中不需要@TargetApi(Build.VERSION_CODES.N) (8认同)
  • 支持库中有什么东西可以避免if-else吗? (5认同)
  • 如果用户安装了多种语言,这可能不起作用。您应该使用`LocaleList.getDefault().get(0);` 因为这将返回按首选语言排序的区域设置。 (4认同)
  • 为什么不直接使用 Locale.getDefault() 呢? (3认同)

Ele*_*gyD 50

如果您使用的是Android支持库,则可以使用ConfigurationCompat而不是@ Makalele的方法来消除删除警告:

Locale current = ConfigurationCompat.getLocales(getResources().getConfiguration()).get(0);
Run Code Online (Sandbox Code Playgroud)

或在Kotlin:

val currentLocale = ConfigurationCompat.getLocales(resources.configuration)[0]
Run Code Online (Sandbox Code Playgroud)

  • 如果没有任何上下文,也可以执行ConfigurationCompat.getLocales(Resources.getSystem()。getConfiguration())。get(0);。 (6认同)

kab*_*uko 13

来自getDefault的文件:

返回用户的首选语言环境.可能已使用setDefault(Locale)覆盖此进程.

同样来自Locale文档:

默认语言环境适用于涉及向用户显示数据的任务.

好像你应该只使用它.

  • 这个答案是错误的,有一个默认的语言环境,但如果用户在飞行中更改它显示错误.下一个答案是对的 (7认同)

小智 8

All answers above - do not work. So I will put here a function that works on 4 and 9 android

private String getCurrentLanguage(){
   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
      return LocaleList.getDefault().get(0).getLanguage();
   } else{
      return Locale.getDefault().getLanguage();
   }
}
Run Code Online (Sandbox Code Playgroud)


Rak*_*oni 8

根据官方文档, 支持库中已弃用ConfigurationCompat

您可以考虑使用

LocaleListCompat.getDefault()[0].toLanguageTag()第 0 个位置将是用户首选区域设置

要在第 0 个位置获取默认语言环境,将是 LocaleListCompat.getAdjustedDefault()