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手动赋予权限,否则会报反射异常

  • 相关阅读:
    享元模式(Flyweight)
    策略模式(strategy)
    访问者模式(Visitor)
    适配器模式(Adapter)
    外观模式(Facade)
    代理模式(Proxy)
    ORACLE 表空间扩展方法
    Oracle XML Publisher
    DB.Package procedure Report
    case ... end 语句
  • 原文地址:https://www.cnblogs.com/mypsq/p/5757891.html
Copyright © 2011-2022 走看看