使用Oreo 8.0手机更改我的应用的语言/区域设置无效

时间:2018-04-07 17:40:45

标签: android android-studio locale android-8.0-oreo programmatically-created

我可以完美地将语言更改为我的应用程序,它适用于所有活动。从来没有遇到任何问题(我的S4和我朋友的手机)。但是当我使用我的S9(Oreo 8.0)时,没有任何作用。对于某些事情,语言会发生变化,但我的应用程序的大部分内容都不会改变语言。我不明白,奥利奥的变化与我所做的不再有效?

这是我要做的改变它(例子是西班牙语):

   private View.OnClickListener btnLangue3Listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            langue.setBackground(ResourcesCompat.getDrawable(getResources(), R.mipmap.spanishflag, null));
  langueMenu.setVisibility(View.GONE);
            if (Locale.getDefault().getLanguage().equals("fr") || Locale.getDefault().getLanguage().equals("en")) {
                Context context = getApplicationContext();
                changeLang(context ,"es");
                saveLocale("es");
                restartActivity();
  }


        }
    };



private void restartActivity() {
        Intent mStartActivity = new Intent(getApplicationContext(), SplashActivity.class);

        finish();
        CharSequence text = getApplicationContext().getResources().getString(R.string.messageLang);
        int duration = Toast.LENGTH_LONG;

        Toast toast = Toast.makeText(this, text, duration);
        toast.show();


        int secs = 14; // Delay in seconds

        Utils.delay(secs, new Utils.DelayCallback() {
            @Override
            public void afterDelay() {
                Intent mStartActivity = new Intent(getApplicationContext(), SplashActivity.class);
                int mPendingIntentId = 123456;
                PendingIntent mPendingIntent = PendingIntent.getActivity(getApplicationContext(), mPendingIntentId, mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
                AlarmManager mgr = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
                mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
                System.exit(0);
            }


        });
    }


 public void loadLocale() {
        String langPref = "Language";
        SharedPreferences prefs = getSharedPreferences("CommonPrefs",
                Activity.MODE_PRIVATE);
        String language = prefs.getString(langPref, "");
        changeLang(getApplicationContext(), language);
    }

    public void changeLang(Context context, String lang) {
        if (lang.equalsIgnoreCase(""))
            return;
        Locale locale = new Locale(lang);
        Locale.setDefault(locale);
        Configuration config = getApplicationContext().getResources().getConfiguration();
        config.setLocale(locale);
        getApplicationContext().createConfigurationContext(config);
        getApplicationContext().getResources().updateConfiguration(config, getApplicationContext().getResources().getDisplayMetrics());
}

    public void saveLocale(String lang) {
        String langPref = "Language";
        SharedPreferences prefs = getSharedPreferences("CommonPrefs",
                Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(langPref, lang);
        editor.commit();
    }

tl; dr:我将语言环境更改为“es”,重新启动活动和应用程序(如果我没有重新启动应用程序,则无法运行)并将其保存在共享首选项中。活动打开后,我检索共享首选项并将其用作以下语言环境: 首先我调用loadLocale();然后:

  public void loadLocale() {
        String langPref = "Language";
        SharedPreferences prefs = getSharedPreferences("CommonPrefs",
                Activity.MODE_PRIVATE);
        String language = prefs.getString(langPref, "");
        changeLang(language);
    }
    public void changeLang(String lang) {


        if (lang.equalsIgnoreCase(""))
            return;
        Locale myLocale = new Locale(lang);
        saveLocale(lang);
        Locale.setDefault(myLocale);


        Configuration config = getApplicationContext().getResources().getConfiguration();
        config.setLocale(myLocale);
        this.createConfigurationContext(config);
        this.getResources().updateConfiguration(config, getApplicationContext().getResources().getDisplayMetrics());
    }

    public void saveLocale(String lang) {
        String langPref = "Language";
        SharedPreferences prefs = getSharedPreferences("CommonPrefs",
                Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(langPref, lang);
        editor.commit();
    }

我做错了什么?有更简单或更好的方法吗?我到处搜索,没有任何效果......

0 个答案:

没有答案