【问题标题】:how to invoke an android internal method with reflection如何使用反射调用android内部方法
【发布时间】:2014-11-24 22:08:47
【问题描述】:

我正在尝试调用一个 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

【问题讨论】:

    标签: java android reflection watchdog


    【解决方案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

    【讨论】:

    • 我注意到 rebootSystem 从 sdk 19 到 sdk 21 是不同的,但是我该如何解决呢?
    猜你喜欢
    • 2011-06-21
    • 2018-02-18
    • 2018-05-08
    • 2019-03-03
    • 1970-01-01
    • 1970-01-01
    • 2014-11-27
    相关资源
    最近更新 更多