我尝试使用代码更改有问题的Android手机语言 但它给了我一个两个例外java.lang.reflect.InvocationTargetException和java.lang.NullPointerException。帮助我任何身体知道如何解决这些异常。
public void changeLanguageSettings(Context con, Locale language) {
try {
//Linguagem definida
Locale locale = language;
Class amnClass = Class.forName("android.app.ActivityManagerNative");
Object amn = null;
Configuration config = null;
// amn = ActivityManagerNative.getDefault();
Method methodGetDefault = amnClass.getMethod("getDefault");
methodGetDefault.setAccessible(true);
amn = methodGetDefault.invoke(amnClass);
// config = amn.getConfiguration();
Method methodGetConfiguration = amnClass
.getMethod("getConfiguration");
methodGetConfiguration.setAccessible(true);
config = (Configuration) methodGetConfiguration.invoke(amn);
// config.userSetLocale = true;
Class configClass = config.getClass();
Field f = configClass.getField("userSetLocale");
f.setBoolean(config, true);
// alterando a linguagem
config.locale = locale;
// amn.updateConfiguration(config);
Method methodUpdateConfiguration = amnClass.getMethod(
"updateConfiguration", Configuration.class);
methodUpdateConfiguration.setAccessible(true);
methodUpdateConfiguration.invoke(amn, config);
} catch (Exception e) {
Log.d("error-->", "" + e.getMessage().toString());
}
}
答案 0 :(得分:1)
如果你想改变用户的默认本地,你可以这样做很难:
Resources res = context.getResources();
// Change locale settings in the app.
DisplayMetrics dm = res.getDisplayMetrics();
android.content.res.Configuration conf = res.getConfiguration();
conf.locale = new Locale(language);
res.updateConfiguration(conf, dm);
答案 1 :(得分:0)
实际上非常简单:
Resources res = context.getResources();
DisplayMetrics dm = res.getDisplayMetrics();
android.content.res.Configuration conf = res.getConfiguration();
conf.locale = new Locale(lang);
res.updateConfiguration(conf, dm);
lang
显然是'en','fr'或'es'等语言代码。