Android初学------系统设置之设置系统语言

经过查看源码得知,原生系统设置系统语言代码调用了一个方法  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);
	}


直接通过反射获取到系统设置语言那个类(LocalePicker),执行类里的updateLocale(Locale locale)这个方法 传入语言设置更新系统语言。感觉简单粗暴。

method[method.length-1]   这个获取到的方法就是updateLocale(Locale locale);


也不知道这样有没有什么不好,总之现在能用就行了。。。。尴尬


注意:需要权限
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION"/>



  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值