Android 应用程序区域设置在 Play 商店版本上不起作用

Jay*_*ena 7 java android locale android-studio

我只需按一下按钮即可更改应用程序区域设置。它可以在 AVD 上完美运行,也可以在具有 API 30 的实际设备上调试和发布构建 APK。

但是,一旦发布,它就无法与应用程序的 Play 商店版本一起使用。区域设置永远不会改变。

请帮忙!谢谢你!

这是SettingsFragment中的代码:

private void setAppLocale(Context context, String language) {
    Locale locale = new Locale(language);
    Locale.setDefault(locale);
    Configuration config = context.getResources().getConfiguration();
    config.setLocale(locale);
    context.createConfigurationContext(config);
    context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
    Intent refresh = new Intent(getActivity().getApplicationContext(), MainActivity.class);
    startActivity(refresh);
    getActivity().finish();
}
Run Code Online (Sandbox Code Playgroud)

一旦按下按钮,上面的代码就会被调用,并且选择也会被放入共享首选项中。活动被刷新并加载主要活动,但区域设置从未更改。

这就是我的 MainActivity 的样子:

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

    hideSystemUI();

    sharedPref = getPreferences(Context.MODE_PRIVATE);
    selectedLanguage = sharedPref.getString("Test.SL.LanguageName", language);
    selectedTheme = sharedPref.getString("Test.SL.ThemeName", "Light");

    if (selectedTheme.equals("Light")){
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    } else if (selectedTheme.equals("Dark")) {
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    }

    if (selectedLanguage.equals("Sinhala")) {
        language = "Sinhala";
        setAppLocale(this, "si");
    } else {
        language = "English";
        setAppLocale(this, "en");
    }

    binding = ActivityMainBinding.inflate(getLayoutInflater());
    setContentView(binding.getRoot());

    setSupportActionBar(binding.appBarMain.toolbar);
    
       ......

}

public void setAppLocale(Context context, String language) {
    Locale locale = new Locale(language);
    Locale.setDefault(locale);
    Configuration config = context.getResources().getConfiguration();
    config.setLocale(locale);
    context.createConfigurationContext(config);
    context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
}
Run Code Online (Sandbox Code Playgroud)

任何想法、建议和解决方案,请!再次感谢你!

小智 0

将其添加到您的应用程序 gradle

android {
    bundle {
        language {
            enableSplit = false
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

详细信息请参见此线程