lundi 2 avril 2018

How to call a function from the inner class using reflection?

Using reflection, I'm trying to call the "disableSelf()" function inside AccessibilityManagerService. The function however is placed inside the inner class Service which extends IAccessibilityServiceConnection.Stub. I'm currently getting InvocationTargetException. Here's my code:

        Class<?> iAccessibilityServiceConnectionClass= Class.forName("android.accessibilityservice.IAccessibilityServiceConnection");
        Class<?> serviceManagerClass = Class.forName("android.os.ServiceManager");

        Class<?>[] classes = iAccessibilityServiceConnectionClass.getDeclaredClasses();
        Class<?> stubClass = null;
        for (Class c : classes) {
            if (c.getSimpleName().equals("Stub")) {
                stubClass = c;
            }
        }


        Method asInterfaceMethod = stubClass.getMethod("asInterface", IBinder.class);
        Method getServiceMethod = serviceManagerClass.getMethod("getService", new Class[]{String.class});

        Method getDisableSelf = iAccessibilityServiceConnectionClass.getMethod("disableSelf");
        Object getAccesibilityConnectionObject = asInterfaceMethod.invoke(null, getServiceMethod.invoke(serviceManagerClass, "accessibility"));
        getDisableSelf.invoke(getAccesibilityConnectionObject);





Aucun commentaire:

Enregistrer un commentaire