代码之家  ›  专栏  ›  技术社区  ›  Mouaad Abdelghafour AITALI

手动更改语言在Android 10上不起作用

  •  0
  • Mouaad Abdelghafour AITALI  · 技术社区  · 4 年前

    Android ( 像素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);
       }
    

    :

      @Override
        protected void attachBaseContext(Context newBase) {
            super.attachBaseContext(ViewPumpContextWrapper.wrap(LocaleUtils.onAttach(newBase)));
        }
    
    0 回复  |  直到 4 年前
        1
  •  0
  •   Leo DroidCoder    4 年前

    甚至在android10之前,我就一直在努力解决三星设备上的动态区域设置更改问题。

    但当时除了你所做的,
    我最终通过以下方式通过资源标识符检索所有字符串:

    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>