how to invoke an android internal method with reflection

久未见 提交于 2020-01-17 03:09:08

问题


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 methods to reboot (involving busybox?).

Class watchdogClass = Class.forName("com.android.server.Watchdog");
Method getInstance = watchdogClass.getDeclaredMethod("getInstance");
Method rebootSystem = watchdogClass.getDeclaredMethod("rebootSystem", String.class);
Object watchdogInstance = getInstance.invoke(null);
rebootSystem.invoke(watchdogInstance, "my reboot message");

This is the exception I get, I googled without finding a solution.

helloroot I/Watchdog﹕ Rebooting system because: my reboot message
helloroot W/System.err﹕ java.lang.reflect.InvocationTargetException
helloroot W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
helloroot W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
helloroot W/System.err﹕ at org.example.helloroot.SettingsActivity.onPostCreate(SettingsActivity.java:80)
helloroot W/System.err﹕ at android.app.Instrumentation.callActivityOnPostCreate(Instrumentation.java:1150)
helloroot W/System.err﹕ at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2188)
helloroot W/System.err﹕ at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
helloroot W/System.err﹕ at android.app.ActivityThread.access$800(ActivityThread.java:144)
helloroot W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
helloroot W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
helloroot W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
helloroot W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5146)
helloroot W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
helloroot W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
helloroot W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
helloroot W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
helloroot W/System.err﹕ at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
helloroot W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
helloroot W/System.err﹕ Caused by: java.lang.ClassCastException: android.os.BinderProxy cannot be cast to com.android.server.power.PowerManagerService
helloroot W/System.err﹕ at com.android.server.Watchdog.rebootSystem(Watchdog.java:302)
helloroot W/System.err﹕ ... 17 more

Watchdog.java source code

Relevant gradle configuration:

android {
    compileSdkVersion 21
    buildToolsVersion '21.0.2'

    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 21

回答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.
 */
void rebootSystem(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.
 */
void rebootSystem(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



来源:https://stackoverflow.com/questions/27115106/how-to-invoke-an-android-internal-method-with-reflection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!