как вызвать внутренний метод Android с отражением

Я пытаюсь вызвать внутренний метод 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

Соответствующая конфигурация градиента:

android {
    compileSdkVersion 21
    buildToolsVersion '21.0.2'

    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 21

person vault    schedule 24.11.2014    source источник


Ответы (1)


Похоже, у вас другая реализация 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

person moh.sukhni    schedule 24.11.2014
comment
Я заметил, что rebootSystem отличается от SDK 19 до SDK 21, но как мне решить эту проблему? - person vault; 25.11.2014