Android 运行时本地化(多语言)在某些设备上不起作用,例如Oppo f9和华为y9

Android 运行时本地化(多语言)在某些设备上不起作用,例如Oppo f9和华为y9,android,android-layout,android-resources,huawei-mobile-services,oppo,Android,Android Layout,Android Resources,Huawei Mobile Services,Oppo,我实现了以下本地化逻辑,以便能够将我的应用程序的语言从英语动态更改为阿拉伯语,反之亦然。它适用于从5.1到9.0的所有emulator版本,以及我的7个物理设备 问题是,我收到专门使用Oppo f9和华为y9(2019)等设备的用户的投诉,他们说当试图更改语言时,布局方向会改变,但应用程序不会使用其他字符串资源。它总是在英语上 我在这件事上疯了,我是不是错过了什么 public class App extends Application { @Override protected void a

我实现了以下本地化逻辑,以便能够将我的应用程序的语言从英语动态更改为阿拉伯语,反之亦然。它适用于从5.1到9.0的所有emulator版本,以及我的7个物理设备

问题是,我收到专门使用Oppo f9和华为y9(2019)等设备的用户的投诉,他们说当试图更改语言时,布局方向会改变,但应用程序不会使用其他字符串资源。它总是在英语上

我在这件事上疯了,我是不是错过了什么

public class App extends Application {

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(LocaleManager.setLocale(base));
}

//Handles screen rotation only up till API 28 pie
@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    LocaleManager.setLocale(this);
    if (Build.VERSION.SDK_INT >= 26) {
        ProcessPhoenix.triggerRebirth(this); //Restart app!
    }
  }
}




}

我能够解决这个问题,方法是生成一个签名的APK(就像我在签名包存在之前通常做的那样),然后将其上传到google play

当一些设备从google play下载应用程序时,使用生成签名包是导致此问题的原因,而其他设备则没有问题,因此令人困惑


我的应用程序大小在某种程度上增加了3MB,但至少当用户从google play(我的和投诉有问题的用户的设备)下载它时,它现在在所有设备上都能正常工作。

我也面临着这个问题。有人能帮忙吗?有人告诉我,它在Honor 5、Lenevo K4 note和其他一些设备中不起作用。@VarunAM我找到了问题的原因。看看下面,看看它是否也适合你。非常感谢你!我花了数周的时间试图弄明白为什么它在某些设备上不起作用。现在,由于您的回答,我终于为所有android用户分发了一个工作版本。如果您仍然需要使用捆绑包构建应用程序,这个回答应该会有所帮助:
public class BaseActivity extends AppCompatActivity {

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

    if (Manager.isFirstLaunch(this)) {  //Check if it is the first time to launch the app
        startActivity(new Intent(this, ChooseLanguageActivity.class));
        finish();
    } else {
        if (Build.VERSION.SDK_INT >= 26) {
            LocaleManager.setLocale(this);
        }
        startActivity(new Intent(this, AuthenticationActivity.class));
        finish();
    }
  }
}
public class ChooseLanguageActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_choose_language);
    Crashlytics.log("ChooseLanguageActivity created");
}

public void EnglishButton(View view) {
    LocaleManager.persistLanguage(this, "en");  //In case the mobile locale is Arabic for API >=Oreo
    LocaleManager.setLocale(this);
    Manager.firstLaunchCompleted(this);
    ProcessPhoenix.triggerRebirth(this); //Restart app!
    finish();
}

public void ArabicButton(View view) {
    LocaleManager.persistLanguage(this, "ar");
    LocaleManager.setLocale(this);
    Manager.firstLaunchCompleted(this);
    ProcessPhoenix.triggerRebirth(this); //Restart app!
    finish();
  }
}
public class LocaleManager {
private static final String SELECTED_LANGUAGE = "Locale.Helper.Selected.Language";
private static final String DEFAULT_LANGUAGE = "en";

public static Context setLocale(Context c) {
    return setNewLocale(c, getLanguage(c));
}

public static Context setNewLocale(Context c, String language) {
    persistLanguage(c, language);
    return updateResources(c, language);
}

public static String getLanguage(Context c) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(c);
    return preferences.getString(SELECTED_LANGUAGE, DEFAULT_LANGUAGE);
}

public static void persistLanguage(Context c, String language) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(c);
    SharedPreferences.Editor editor = preferences.edit();
    editor.putString(SELECTED_LANGUAGE, language);
    editor.commit(); //U must use Commit and not apply, to avoid opening a new thread, causing a delayed writting in a separate thread!
    Manager.appLanguage=language;//edited upon lang selection screen/future app launches/lang change through menu
}


private static Context updateResources(Context context, String language) {
    Locale locale = new Locale(language);
    Locale.setDefault(locale);

    Resources res = context.getResources();
    Configuration config = new Configuration(res.getConfiguration());

    if(Build.VERSION.SDK_INT >= 17) {
        config.setLocale(locale);
        res.updateConfiguration(config, res.getDisplayMetrics());
    }

    return context;
}