Locale.getDefault()在某些Android设备上已过时 [英] Locale.getDefault() is outdated on some Android devices

查看:50
本文介绍了Locale.getDefault()在某些Android设备上已过时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一个广播接收器,当设备的区域设置更改时会被调用.我的应用中的某些设备上存在错误,特别是Nexus 5x和Galaxy S8 +(以及很有可能是其他设备)上的错误,其中Locale.getDefault()返回陈旧/先前的值.本地化的字符串针对新语言正确显示,但是在应用程序内部,我们的语言环境已过时.有什么想法吗?

I have a broadcast receiver in my app that gets called when the device's locale changes. There is a bug in my app on a few devices, specifically the Nexus 5x and Galaxy S8+ (and very likely others) where Locale.getDefault() returns the stale/previous value. The localized strings are displaying correctly for the new language, but inside the application, our locale is outdated. Any ideas?

例如:

1)设备为英文->应用区域设置告诉我们我们使用的是英语.

2) 后台应用并将设备语言更改为德语

3)应用接收到更改了语言环境的广播事件,Locale.getDefault()返回英语而不是德语

我看到德语在这里返回了很多设备,但不是所有设备.

I see German being returned here for many devices, but not all devices.

推荐答案

另一个答案所述,定义了默认语言环境在创建应用程序时静态 .尝试直接从您的资源中检索语言环境.

As referenced in another answer, the default locale is defined statically at the time the application is created. Try retrieving the locale from your Resources directly.

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

这篇关于Locale.getDefault()在某些Android设备上已过时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆