天天看點

用反射來調用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;         }