Locale.getDefault() is outdated on some Android devices

你。 提交于 2021-01-28 02:21:59

问题


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?

ex:

1) Device is in English -> App locale tells us we are in English.

2) Background the app and change the device language to German

3) App receives the locale changed broadcast event, Locale.getDefault() returns English instead of German

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


回答1:


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;
}


来源:https://stackoverflow.com/questions/45556358/locale-getdefault-is-outdated-on-some-android-devices

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!