如何调用与反思的机器人内部方法 [英] how to invoke an android internal method with reflection

查看:267
本文介绍了如何调用与反思的机器人内部方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打电话给一个机器人内部的方法来重新启动设备。这只是一个实验,我会试着去了解我在做什么错。我知道有可能是更好的方法来重新启动(涉及busybox的?)。

 类watchdogClass =的Class.forName(com.android.server.Watchdog);
方法getInstance = watchdogClass.getDeclaredMethod(的getInstance);
方法rebootSystem = watchdogClass.getDeclaredMethod(rebo​​otSystem,为String.class);
对象watchdogInstance = getInstance.invoke(NULL);
rebootSystem.invoke(watchdogInstance,我的重启消息);

这是我得到的异常,我用Google搜索没有找到一个解决方案。

  helloroot I /看门狗:重新启动系统,因为:我重启消息
helloroot W / System.err的:java.lang.reflect.InvocationTargetException
helloroot W / System.err的:在java.lang.reflect.Method.invokeNative(本机方法)
helloroot W / System.err的:在java.lang.reflect.Method.invoke(Method.java:515)
helloroot W / System.err的:在org.example.helloroot.SettingsActivity.onPostCreate(SettingsActivity.java:80)
helloroot W / System.err的:在android.app.Instrumentation.callActivityOnPostCreate(Instrumentation.java:1150)
helloroot W / System.err的:在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2188)
helloroot W / System.err的:在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
helloroot W / System.err的:在android.app.ActivityThread.access $ 800(ActivityThread.java:144)
helloroot W / System.err的:在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1205)
helloroot W / System.err的:在android.os.Handler.dispatchMessage(Handler.java:102)
helloroot W / System.err的:在android.os.Looper.loop(Looper.java:136)
helloroot W / System.err的:在android.app.ActivityThread.main(ActivityThread.java:5146)
helloroot W / System.err的:在java.lang.reflect.Method.invokeNative(本机方法)
helloroot W / System.err的:在java.lang.reflect.Method.invoke(Method.java:515)
helloroot W / System.err的:在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:732)
helloroot W / System.err的:在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
helloroot W / System.err的:在de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
helloroot W / System.err的:在dalvik.system.NativeStart.main(本机方法)
helloroot W / System.err的:致:java.lang.ClassCastException:android.os.BinderProxy不能转换为com.android.server.power.PowerManagerService
helloroot W / System.err的:在com.android.server.Watchdog.rebootSystem(Watchdog.java:302)
helloroot W / System.err的:... 17个

Watchdog.java源$ C ​​$ C

相关配置的gradle:

 安卓{
    compileSdkVersion 21
    buildToolsVersion '21 .0.2    defaultConfig {
        19的minSdkVersion
        targetSdkVersion 21


解决方案

看来你的设备上有不同的SDK实现
checkthis实现的方法rebootSystem(字符串雷森)的:

<一个href=\"https://android.googlesource.com/platform/frameworks/base.git/+/android-4.2.2_r1/services/java/com/android/server/Watchdog.java\" rel=\"nofollow\">https://android.googlesource.com/platform/frameworks/base.git/+/android-4.2.2_r1/services/java/com/android/server/Watchdog.java

  / **
 *执行系统的全面重启。
 * /
无效rebootSystem(字符串的原因){
    Slog.i(TAG,重新启动系统,因为:+原因);
    PowerManagerService PMS =(PowerManagerService)ServiceManager.getService(权力);
    pms.reboot(假的,因此,FALSE);
}

和您对您的问题提供了源$ C ​​$ C的实现:

  / **
 *执行系统的全面重启。
 * /
无效rebootSystem(字符串的原因){
    Slog.i(TAG,重新启动系统,因为:+原因);
    IPowerManager PMS =(IPowerManager)ServiceManager.getService(Context.POWER_SERVICE);
    尝试{
        pms.reboot(假的,因此,FALSE);
    }赶上(RemoteException的前){
    }
}

这就是为什么你得到ClassCastException异常:

helloroot W / System.err的:java.lang.ClassCastException:致android.os.BinderProxy不能转换为com.android.server.power.PowerManagerService

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

解决方案

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

这篇关于如何调用与反思的机器人内部方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆