java - 手动更改语言在三星设备上不起作用

标签 java android locale android-10.0

我在手动更改应用程序语言时遇到了麻烦,在应用程序中,我为用户提供了将应用程序语言更改为他们喜欢的语言的功能,下面的代码即使在 Android 中也能正常工作(< strong>Pixel 3 模拟器),但由于某种原因,它无法在所有三星设备上运行

            Context context = LocaleUtils.setLocale(getApplicationContext(), languageCode);
            Resources resources = context.getResources();
            Locale myLocale = new Locale(languageCode);
            DisplayMetrics dm = resources.getDisplayMetrics();
            Configuration conf = resources.getConfiguration();
            conf.locale = myLocale;
            resources.updateConfiguration(conf, dm);
            Intent intent = getBaseContext().getPackageManager().getLaunchIntentForPackage(
                    getBaseContext().getPackageName());
            if (intent != null) {
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intent);
            }

应用程序类:

 @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        LocaleUtils.onAttach(base, Locale.getDefault().getLanguage());
        MultiDex.install(this);
   }

每个 Activity :

  @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(ViewPumpContextWrapper.wrap(LocaleUtils.onAttach(newBase)));
    }

最佳答案

甚至在 Android 10 之前,我就一直在努力解决三星设备上的动态区域设置更改问题。
现在可能有更好的解决方案,
但当时除了你所做的之外,
我最终通过以下方式通过资源标识符检索所有字符串:

public static String getStringByIdentifier(final Context context, final String stringResIdName, final boolean forceRefresh) {
        final Resources res = getResources(context, forceRefresh);
        String result;
        try {
            result = res.getString(res.getIdentifier(stringResIdName, "string",
                    context.getPackageName()));
        } catch (final Resources.NotFoundException e) {
            result = stringResIdName; // TODO or here you may throw an Exception and handle it accordingly. 
        }
        return result;
    }

public static String getStringByIdentifier(final Context context, final String stringResIdName) {
        return getStringByIdentifier(context, stringResIdName, false);
    }

 private static Resources getResources(final Context context, final boolean refreshLocale) {
        if (!refreshLocale) {
            return context.getResources();
        } else {
            final Configuration configuration = new Configuration(context.getResources().getConfiguration());
            configuration.setLocale(Locale.getDefault());
            return context.createConfigurationContext(configuration).getResources();
        }
    }

所以你必须用下面的方式设置文本:

textView.setText(AndroidUtils.getStringByIdentifier(context, "string_res_name"));

对应的字符串资源在哪里:

<string name="string_res_name">Some string</string>

关于java - 手动更改语言在三星设备上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60591631/

相关文章:

java - 如何使用 CMU Sphinx 将音频文件转换为文本

java - 为什么 write() 和 read() 似乎写入和读取超过一个字节?

Android Studio 无法运行 AVD

C++ 字符串小写与自定义语言环境

Java NumberFormat : Missing Currency Symbols for Portugal, 卢森堡等

java - 有没有办法在不使用函数的情况下在文件中写入二维矩阵?

java - java util 类中的调试点

android - 获取依赖运行时列表

android:在sqlite数据库中缓存gson数据

java - 为 fragment 中的 TextView 设置语言环境