Skip to content Skip to sidebar Skip to footer

How To Invoke An Android Internal Method With Reflection

I'm trying to call an android internal method to reboot the device. It's just an experiment, I would try to understand what I'm doing wrong. I know there are probably better method

Solution 1:

It seems that you have different SDK implementation on your device checkthis implementation of the method rebootSystem(String resen) on:

https://android.googlesource.com/platform/frameworks/base.git/+/android-4.2.2_r1/services/java/com/android/server/Watchdog.java

/**
 * Perform a full reboot of the system.
 */voidrebootSystem(String reason) {
    Slog.i(TAG, "Rebooting system because: " + reason);
    PowerManagerService pms = (PowerManagerService) ServiceManager.getService("power");
    pms.reboot(false, reason, false);
}

and the implementation in the source code you provided on your question:

/**
 * Perform a full reboot of the system.
 */voidrebootSystem(String reason) {
    Slog.i(TAG, "Rebooting system because: " + reason);
    IPowerManager pms = (IPowerManager)ServiceManager.getService(Context.POWER_SERVICE);
    try {
        pms.reboot(false, reason, false);
    } catch (RemoteException ex) {
    }
}

that's why you got ClassCastException:

helloroot W/System.err﹕ Caused by: java.lang.ClassCastException: android.os.BinderProxy cannot be cast to com.android.server.power.PowerManagerService

Post a Comment for "How To Invoke An Android Internal Method With Reflection"