经过查看源码得知,原生系统设置系统语言代码调用了一个方法 com.android.internal.app.LocalePicker类里面的 如下方法:
/**
* Requests the system to update the system locale. Note that the system looks halted
* for a while during the Locale migration, so the caller need to take care of it.
*/
public static void updateLocale(Locale locale) {
try {
IActivityManager am = ActivityManagerNative.getDefault();
Configuration config = am.getConfiguration();
// Will set userSetLocale to indicate this isn't some passing default - the user
// wants this remembered
config.setLocale(locale);
am.updateConfiguration(config);
// Trigger the dirty bit for the Settings Provider.
BackupManager.dataChanged("com.android.providers.settings");
} catch (RemoteException e) {
// Intentionally left blank
}
}
但是呢 由于IActivityManager和ActivityManagerNative类我们调用不了,所以这个方法 也不能直接拿出来使用,看到网上有的通过反射获取到这2个类实例然后更改系统语言,
但是我用的时候出现一些问题,也不知道是不是用得不对,然后就自己实践出一种方法如下:
public void updateLocale(Locale locale) throws Exception {
// LocalePicker.updateLocale(locale);
Object obj = null;
Class<?> demo;
demo = Class.forName("com.android.internal.app.LocalePicker");
obj = demo.newInstance();
Method method[];
method = demo.getDeclaredMethods();
method[method.length-1].invoke(demo, locale);
}
method[method.length-1] 这个获取到的方法就是updateLocale(Locale locale);
也不知道这样有没有什么不好,总之现在能用就行了。。。。
注意:需要权限
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION"/>