Android:更改语言Android 8(o),在后台应用程序时未更新活动资源

发布于 2025-02-10 06:50:22 字数 2863 浏览 0 评论 0原文

操作:sendBroadcast action_locale_changed:

我在actecte_locale_changed中注册了广播()。一旦收到广播接收器,将调用()方法。 要使用以下两个API进行更改的语言环境。 a)getApplicationContext()。getResources()。getConfiguration() b)this.getResources()。

从API(a)我能够获得正确的语言环境。但是从API(b),我无法获得更新/正确的语言环境,它总是给以前的语言环境。

以下是我尝试的步骤:

  1. 打开testapplication-> locale在“ en”(系统环境也“ en en”)中
  2. ,按主机按钮,应用程序转到了后台。
  3. 我将系统语言环境从“ EN”更改为“ JA”。
  4. 所有系统都将场所更改为“ JA”,
  5. 我再次打开了测试应用程序(即从背景到前景的testapplication),
  6. 但是该语言环境没有更改,它仍在以前的语言环境中)

下面的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:

  1. Open the Testapplication ->locale is in "en" (System locale also "en")
  2. Press home button, application went to background.
  3. I changed the system locale from "en" to "ja".
  4. All the systems has changed the locale to "ja"
  5. Again I opened the Testapplication (i.e Testapplication coming from background to Foreground)
  6. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文