天天看点

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

主要是利用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;

    }      
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;      
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;
    }      
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;         }