zoukankan      html  css  js  c++  java
  • Android利用Java反射机制修改Android System Language

    private void updateLanguage(Locale locale) {
            try {
                Object objIActMag, objActMagNative;
                Class clzIActMag = Class.forName("android.app.IActivityManager");
                Class clzActMagNative = Class.forName("android.app.ActivityManagerNative");
                Method mtdActMagNative$getDefault = clzActMagNative.getDeclaredMethod("getDefault");
                // IActivityManager iActMag = ActivityManagerNative.getDefault();
                objIActMag = mtdActMagNative$getDefault.invoke(clzActMagNative);
                // Configuration config = iActMag.getConfiguration();
                Method mtdIActMag$getConfiguration = clzIActMag.getDeclaredMethod("getConfiguration");
                Configuration config = (Configuration) mtdIActMag$getConfiguration.invoke(objIActMag);
                config.locale = locale;
                // iActMag.updateConfiguration(config);
                // 此处需要声明权限:android.permission.CHANGE_CONFIGURATION
                // 会重新调用 onCreate();
                Class[] clzParams = { Configuration.class };
                Method mtdIActMag$updateConfiguration = clzIActMag.getDeclaredMethod(
                        "updateConfiguration", clzParams);
                mtdIActMag$updateConfiguration.invoke(objIActMag, config);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    参数从Local中获取,例如:Local.ENGLISH;

    使用之前应该root系统,adb shell

    ->su

    然后# pm grant com.example.clickdemo android.permission.CHANGE_CONFIGURATION手动赋予权限,否则会报反射异常

  • 相关阅读:
    LDA模型了解及相关知识
    GAN
    tensorflow学习6
    GAN的文献综述
    python相关工具
    tensorflow学习5----变量管理
    tensorflow学习5----GAN模型初探
    8月14日至8月20日技术积累
    8月7日至8月13日技术积累
    8月1日到8月6日技术要点
  • 原文地址:https://www.cnblogs.com/mypsq/p/5757891.html
Copyright © 2011-2022 走看看