问题
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