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

时间:2014-11-24 22:08:48

标签: java android reflection watchdog

我正在尝试调用android内部方法来重启设备。这只是一个实验,我会试着去理解我做错了什么。我知道有更好的方法可以重启(涉及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");

这是我得到的例外,我用谷歌搜索而没有找到解决方案。

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

相关的gradle配置:

android {
    compileSdkVersion 21
    buildToolsVersion '21.0.2'

    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 21

1 个答案:

答案 0 :(得分:0)

您的设备上似乎有不同的SDK实现 检查方法rebootSystem(String resen)的实现方法:

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);
}

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

/**
 * 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