用反射来调用android中的隐藏类

来源:互联网 发布:ios腾讯新闻完整源码 编辑:程序博客网 时间:2024/06/09 23:41
主要是利用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;    

    }主要是利用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
原创粉丝点击