android - 如何获取当前语言环境(API 级别 24)?

标签 android locale android-7.0-nougat

我是这样做的:

context.getResources().getConfiguration().locale

Configuration.locale如果目标是 24,则不推荐使用。所以我做了这个更改:

context.getResources().getConfiguration().getLocales().get(0)

现在它说它只适用于 minSdkVersion 24,所以我不能使用它,因为我的最小目标较低。

什么是正确的方法?

最佳答案

检查您正在运行的版本并退回到已弃用的解决方案:

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

关于android - 如何获取当前语言环境(API 级别 24)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38267213/

相关文章:

javascript - 值不能从 ReadableNativeMap 转换为 double

android - 如何在 Android N 中允许多窗口功能 Activity 明智?

java - 如何检测它是最近按钮、多窗口模式还是主页按钮 onUserLeaveHint()

使用 build.gradle 的 android studio 地址 sanitizer

android - 如何更改android `temp`的 `sdk`目录路径?

android - 在 Phonegap 3/Cordova 3 中检索用户 sim 号码

c - HPUX 上的 wcwidth 问题 : Internationalization and Localization Issue on HPUX

javascript - Jquery datepicker 和 Spring MVC 国际化

spring - 如何重用Spring或Java的语言环境查找代码?

android - 禁用 Android N 的多窗口功能不适用于 Activity