Java 以编程方式更改区域设置在Android 6.0.1上不起作用

Java 以编程方式更改区域设置在Android 6.0.1上不起作用,java,android,Java,Android,我在将语言更改为Android 6.0.1时遇到了一个问题,在新的Android版本中,更改语言运行良好,但在6.0.1中,无论设备设置的语言是什么,都设置默认字符串。仿真器可以工作,但当我在三星J5上安装apk时,它在Android 6.0.1上确实可以工作,而change language不起作用。我的问题有什么解决办法吗?多谢各位 private void setLocale(String lang) { if (Build.VERSION.SDK_INT >= Build.VE

我在将语言更改为Android 6.0.1时遇到了一个问题,在新的Android版本中,更改语言运行良好,但在6.0.1中,无论设备设置的语言是什么,都设置默认字符串。仿真器可以工作,但当我在三星J5上安装apk时,它在Android 6.0.1上确实可以工作,而change language不起作用。我的问题有什么解决办法吗?多谢各位

 private void setLocale(String lang) {
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
    Locale locale = new Locale(lang);
    Locale.setDefault(locale);
    Configuration config = 
    getBaseContext().getResources().getConfiguration();
    config.locale = locale;
    getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());

} else {
    Resources resources = getBaseContext().getResources();
    Configuration configuration = resources.getConfiguration();
    configuration.setLocale(new Locale(lang));
    getBaseContext().getApplicationContext().createConfigurationContext(configuration);
}

// shared pref.
SharedPreferences.Editor editor = getSharedPreferences("Settings", MODE_PRIVATE).edit();
editor.putString("My_Lang", lang);
editor.apply();

}

请检查,以下链接可能会有所帮助:


由于我设法编辑了代码,所以关闭了主题,可能有人需要它

编辑

private void setLocale(String lang) {
    Locale locale = new Locale(lang);
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    if (Build.VERSION.SDK_INT >= 24) {
        config.setLocale(locale);
        getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
    } else {
        config.locale = locale;
        getBaseContext().getApplicationContext().createConfigurationContext(config);
    }

    // shared pref.
    SharedPreferences.Editor editor = getSharedPreferences("Settings", MODE_PRIVATE).edit();
    editor.putString("My_Lang", lang);
    editor.apply();
}

这对我不起作用。我尝试了所有的解决方案,但没有在安卓6.0.1上工作

我正在使用appCompat
androidx。appCompat:appCompat:1.1.0

请看这个

我使用了下面的代码,它在所有版本上都能完美地工作

@Override
public void applyOverrideConfiguration(Configuration overrideConfiguration) {
    if (overrideConfiguration != null) {
        int uiMode = overrideConfiguration.uiMode;
        overrideConfiguration.setTo(getBaseContext().getResources().getConfiguration());
        overrideConfiguration.uiMode = uiMode;
    }
    super.applyOverrideConfiguration(overrideConfiguration);
}

如果您使用aap分发应用程序,请确保所有语言字符串都捆绑在用户从Google Play下载的apk中

您可以通过添加以下内容更新应用程序build.gradle:

android {
  bundle {
     language {
     // Specifies that the app bundle should not support
     // configuration APKs for language resources. These
     // resources are instead packaged with each base and
     // dynamic feature APK.
     enableSplit = false
     }
  }
}

这个答案基于2021年3月

我使用了下面的代码,它在所有版本上都能完美地工作

完整代码:

android {
  bundle {
     language {
     // Specifies that the app bundle should not support
     // configuration APKs for language resources. These
     // resources are instead packaged with each base and
     // dynamic feature APK.
     enableSplit = false
     }
  }
}
注意:对于6.0.1版本,必须在
setContentView(R.layout.activity_main)
之前更新区域设置;对于6.0.1的超级版本(API级别>=24),需要在项目的所有活动中覆盖
受保护的void attachBaseContext(Context newBase)

main活动

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            ContextUtils.updateLocale(MainActivity.this, ContextUtils.getSavedLanguage(MainActivity.this));
        }

        setContentView(R.layout.activity_main);
        ......
        .....
}

@Override
    protected void attachBaseContext(Context newBase) {
        String localeToSwitchTo= ContextUtils.getSavedLanguage(newBase);
        ContextWrapper localeUpdatedContext = ContextUtils.updateLocale(newBase, localeToSwitchTo);
        super.attachBaseContext(localeUpdatedContext);
    }

updateConfiguration也不推荐用于androids>=N