Android修改系统语言

android很多app的语言设置都是设置自己app的语言,并没有设置到系统的语言,今天小编小试牛刀,自己实现了一个可以设置androi系统语言的功能。如下:

Locale locale = Locale.ENGLISH;//英文
Locale locale = Locale.CHINA;//中文

                Class amnClass = null;
                try {
                    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);

                    // set the locale to the new value
                    config.locale = locale;

                    // amn.updateConfiguration(config);
                    Method methodUpdateConfiguration = amnClass.getMethod("updateConfiguration", Configuration.class);
                    methodUpdateConfiguration.setAccessible(true);
                    methodUpdateConfiguration.invoke(amn, config);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }

最后在AndroidManifest.xml里设置:
这里写图片描述

完结!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值