android 反射invoke隐藏API(not verified)

http://blog.csdn.net/nexttake/article/details/8481147

http://blog.csdn.net/gumanren/article/details/7014283


Setting中设置语言的代码


[java]  view plain copy
  1. public static void updateLocale(Locale locale) {  
  2.        try {  
  3.            IActivityManager am = ActivityManagerNative.getDefault();  
  4.            Configuration config = am.getConfiguration();  
  5.   
  6.            config.locale = locale;  
  7.   
  8.            // indicate this isn't some passing default - the user wants this remembered  
  9.            config.userSetLocale = true;  
  10.   
  11.            am.updateConfiguration(config);  
  12.            // Trigger the dirty bit for the Settings Provider.  
  13.            BackupManager.dataChanged("com.android.providers.settings");  
  14.        } catch (RemoteException e) {  
  15.            // Intentionally left blank  
  16.        }  
  17.    }  

反射

[java]  view plain copy
  1. private void updateLanguage(Locale locale) {    
  2.      Log.d("yzy", locale.toString());    
  3.      try {    
  4.          Object objIActMag;    
  5.          Class clzIActMag = Class.forName("android.app.IActivityManager");    
  6.          Class clzActMagNative = Class.forName("android.app.ActivityManagerNative");    
  7.          Method mtdActMagNative$getDefault = clzActMagNative.getDeclaredMethod("getDefault");    
  8.          // IActivityManager iActMag = ActivityManagerNative.getDefault();    
  9.          objIActMag = mtdActMagNative$getDefault.invoke(clzActMagNative);    
  10.          // Configuration config = iActMag.getConfiguration();    
  11.          Method mtdIActMag$getConfiguration = clzIActMag.getDeclaredMethod("getConfiguration");    
  12.          Configuration config = (Configuration) mtdIActMag$getConfiguration.invoke(objIActMag);    
  13.          config.locale = locale;   
  14.          config.userSetLocale = true;  
  15.          // iActMag.updateConfiguration(config);    
  16.          // 此处需要声明权限:android.permission.CHANGE_CONFIGURATION    
  17.          // 会重新调用 onCreate();    
  18.          Class[] clzParams = { Configuration.class };    
  19.          Method mtdIActMag$updateConfiguration = clzIActMag.getDeclaredMethod(    
  20.                  "updateConfiguration", clzParams);    
  21.          mtdIActMag$updateConfiguration.invoke(objIActMag, config);    
  22.            
  23.            
  24. /          BackupManager.dataChanged("com.android.providers.settings");  
  25.          Class clzBackupManager = Class.forName("android.app.backup.BackupManager");  
  26.          Class[] clzString= {String.class};  
  27.          Method mtdDataChanged = clzBackupManager.getDeclaredMethod("dataChanged", clzString);  
  28.          mtdDataChanged.invoke(clzBackupManager, "com.android.providers.settings");  
  29.            
  30.      } catch (Exception e) {    
  31.          e.printStackTrace();    
  32.      }    
  33.  }    



------------------------------------------------------------------------------

主要是利用java 中java.lang.Object下的Method类

Method提供关于类或接口上单独某个方法(以及如何访问该方法)的信息。所反映的方法可能是类方法或实例方法(包括抽象方法)。

Method允许在匹配要调用的实参与基础方法的形参时进行扩展转换;但如果要进行收缩转换,则会抛出IllegalArgumentException。

http://www.cjsdn.net/Doc/JDK50/
//

例如:该function 需要 “Queries the framework about whether any physical keys exist on the
 any keyboard attached to the device that are capable of producing the given   array of key codes.“
 public static boolean[] deviceHasKeys(int[] keyCodes) {
        boolean[] ret = new boolean[keyCodes.length];
        IWindowManager wm = IWindowManager.Stub.asInterface(ServiceManager.getService("window"));
        try {
            wm.hasKeys(keyCodes, ret);
        } catch (RemoteException e) {
            // no fallback; just return the empty array
        }
        return ret;
    }

其中 IWindowManager ServiceManageer均为隐藏类,

要想这样用,有两个方法:

1是修改framwork 让其不为hide class 从而可以使用

2是使用java的映射机制。

下面是使用映射后,对应的代码:

 public static boolean[] deviceHasKeys(int[] keyCodes) {
        boolean[] ret = new boolean[keyCodes.length];
        Method method;
        String methodName = "hasKeys";//haskey 为隐藏类的隐藏method
         try {
           method = Class.forName("android.view.IWindowManager.Stub").getMethod(methodName, String.class);
            try {
                method.invoke(Class.forName("android.view.IWindowManager.Stub"),keyCodes,ret);
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }    
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return ret;    

    }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值