Android:更改语言Android 8(o),在后台应用程序时未更新活动资源
操作:sendBroadcast action_locale_changed:
我在actecte_locale_changed中注册了广播()。一旦收到广播接收器,将调用()方法。 要使用以下两个API进行更改的语言环境。 a)getApplicationContext()。getResources()。getConfiguration() b)this.getResources()。
从API(a)我能够获得正确的语言环境。但是从API(b),我无法获得更新/正确的语言环境,它总是给以前的语言环境。
以下是我尝试的步骤:
- 打开testapplication-> locale在“ en”(系统环境也“ en en”)中
- ,按主机按钮,应用程序转到了后台。
- 我将系统语言环境从“ EN”更改为“ JA”。
- 所有系统都将场所更改为“ JA”,
- 我再次打开了测试应用程序(即从背景到前景的testapplication),
- 但是该语言环境没有更改,它仍在以前的语言环境中)
下面的localechanged(),每当收到action_locale_changed broadcastreceiver收到action_locale_locale_change_changed()。那个时间应用在后台。
在Android 6和7中,this.getResources()。getConfiguration()能够携带正确的资源或语言环境。但是,当Action_locale_changed Broadcastreceiver收到Action_locale_change_locale_changed,Android 8及以上我们会得到这个问题(不更改)。
可以在
ConconfigurationChanged()
在Android模拟器本身中,我能够在使用onConfigurationChanged()时
上看到此问题。尽管我可以通过onConfigurationChanged()获得更新/更正的语言环境,但是我想知道为什么它不通过action_locale_changed broadcast并使用this.getResources()。getConfiguration()。
任何人都可以建议在那里有任何API差异Android 7.1和Android 8
。
以下是源代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.i(TAG,"onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter testLocaleChangedFilter = new IntentFilter();
testLocaleChangedFilter.addAction(Intent.ACTION_LOCALE_CHANGED);
getApplicationContext().registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
localeChanged();
}
}, testLocaleChangedFilter);
}
localechanged()方法,当broadcastreceiver()带有操作:action_locale_changed
private void localeChanged() {
Configuration config = getApplicationContext().getResources().getConfiguration();
Log.i(TAG, "localeChanged - getApplicationContext().getResources().getConfiguration():" + config);
Configuration config2 = this.getResources().getConfiguration();
Log.i(TAG, "localeChanged - this.getResources().getConfiguration():" + config2.locale);
((TextView)findViewById(R.id.text_language)).setText(R.string.language_text);
Log.i(TAG,"getResources().getString(R.string.language_text):" + getResources().getString(R.string.language_text));}
@Override
public void onConfigurationChanged(android.content.res.Configuration newConfig) {
Log.i(TAG, "onConfigurationChanged");
super.onConfigurationChanged(newConfig);
String locale = Locale.getDefault().toString();
Log.i(TAG, "onConfigurationChanged(Locale):" + locale);
Configuration config2 = this.getResources().getConfiguration();
Log.i(TAG, "onConfigurationChanged-this.getResources().getConfiguration():" + config2.locale);}
action:sendBroadcast ACTION_LOCALE_CHANGED:
I registered the broadcast in the onCreate() for ACTION_LOCALE_CHANGED. Once broadcast receiver received and localeChanged() method will call.
To get the changed locale using the below two APIs.
A) getApplicationContext().getResources().getConfiguration()
B) this.getResources().getConfiguration()
From the API (A) I can able to get the correct locale. But from the API (B) I could n't get able to get the updated/correct locale, its always giving the previous locale.
Below are the steps I tried:
- Open the Testapplication ->locale is in "en" (System locale also "en")
- Press home button, application went to background.
- I changed the system locale from "en" to "ja".
- All the systems has changed the locale to "ja"
- Again I opened the Testapplication (i.e Testapplication coming from background to Foreground)
- But the locale not changed, its still in the previous locale)
The below localeChanged() called whenever the ACTION_LOCALE_CHANGED broadcastreceiver received. That time application was in background.
In Android 6 and 7 this.getResources().getConfiguration() could able to bring the correct resources or locale. But android 8 and above we get this problem(locale not changed) when ACTION_LOCALE_CHANGED broadcastreceiver received.
In android emulator itself I can able to see this issue
onConfigurationChanged():
The above scenario working while using onCOnfigurationchanged().
Eventhough I can get the updated/correct locale through onConfigurationChanged(), but I want to know why its not getting the updated locale via ACTION_LOCALE_CHANGED broadcast and using this.getResources().getConfiguration() API.
Can anyone suggest any API differences are there android 7.1 and android 8.
Thanks in Advance!
Below are the source codes:
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.i(TAG,"onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter testLocaleChangedFilter = new IntentFilter();
testLocaleChangedFilter.addAction(Intent.ACTION_LOCALE_CHANGED);
getApplicationContext().registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
localeChanged();
}
}, testLocaleChangedFilter);
}
LocaleChanged() method called when BroadcastReceiver() with Action: ACTION_LOCALE_CHANGED
private void localeChanged() {
Configuration config = getApplicationContext().getResources().getConfiguration();
Log.i(TAG, "localeChanged - getApplicationContext().getResources().getConfiguration():" + config);
Configuration config2 = this.getResources().getConfiguration();
Log.i(TAG, "localeChanged - this.getResources().getConfiguration():" + config2.locale);
((TextView)findViewById(R.id.text_language)).setText(R.string.language_text);
Log.i(TAG,"getResources().getString(R.string.language_text):" + getResources().getString(R.string.language_text));}
@Override
public void onConfigurationChanged(android.content.res.Configuration newConfig) {
Log.i(TAG, "onConfigurationChanged");
super.onConfigurationChanged(newConfig);
String locale = Locale.getDefault().toString();
Log.i(TAG, "onConfigurationChanged(Locale):" + locale);
Configuration config2 = this.getResources().getConfiguration();
Log.i(TAG, "onConfigurationChanged-this.getResources().getConfiguration():" + config2.locale);}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论