Java 如何使用反射调用android内部方法

Java 如何使用反射调用android内部方法,java,android,reflection,watchdog,Java,Android,Reflection,Watchdog,我正在尝试调用android内部方法来重新启动设备。这只是一个实验,我会尽力去理解我做错了什么。我知道可能有更好的重启方法(包括busybox?) 这是我得到的一个例外,我在谷歌上搜索没有找到解决方案 helloroot I/Watchdog﹕ Rebooting system because: my reboot message helloroot W/System.err﹕ java.lang.reflect.InvocationTargetException helloroot W/Sys

我正在尝试调用android内部方法来重新启动设备。这只是一个实验,我会尽力去理解我做错了什么。我知道可能有更好的重启方法(包括busybox?)

这是我得到的一个例外,我在谷歌上搜索没有找到解决方案

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

相关梯度配置:

android {
    compileSdkVersion 21
    buildToolsVersion '21.0.2'

    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 21

您的设备上似乎有不同的SDK实现 在以下位置检查方法rebootSystem(字符串重新设置)的此实现:

以及您就问题提供的源代码中的实现:

/**
 * 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) {
    }
}
这就是为什么你有ClassCastException:


helloroot W/System.err﹕ 原因:java.lang.ClassCastException:android.os.BinderProxy无法强制转换为com.android.server.power.PowerManagerService

我注意到rebootSystem与sdk 19和sdk 21不同,但如何解决这个问题?
 /**
 * 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);
}
/**
 * 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) {
    }
}