前言
记录一下Android语言切换,网上也很多,这里摘抄于此方便自己查阅
正文
下面代码都测试过,验证ok
方式一
private void language(Locale locale) {
try {
LocalePicker.updateLocale(locale == Locale.ENGLISH ? Locale.SIMPLIFIED_CHINESE : Locale.ENGLISH);
} catch (Exception e) {
e.printStackTrace();
}
}
方式二
反射
private void language(Locale locale) {
try {
Class iActivityManager = Class.forName("android.app.IActivityManager");
Class activityManagerNative = Class.forName("android.app.ActivityManagerNative");
Method getDefault = activityManagerNative.getDeclaredMethod("getDefault");
Object objIActMag = getDefault.invoke(activityManagerNative);
Method getConfiguration = iActivityManager.getDeclaredMethod("getConfiguration");
Configuration config = (Configuration) getConfiguration.invoke(objIActMag);
config.locale = locale;
Class clzConfig = Class.forName("android.content.res.Configuration");
java.lang.reflect.Field userSetLocale = clzConfig.getField("userSetLocale");
userSetLocale.set(config, true);
Class[] clzParams = {Configuration.class};
Method updateConfiguration = iActivityManager.getDeclaredMethod("updateConfiguration", clzParams);
updateConfiguration.invoke(objIActMag, config);
// BackupManager.dataChanged("com.android.providers.settings");
} catch (Exception e) {
e.printStackTrace();
}
}
参考文章
© 版权声明