title: how to invoke an android internal method with reflection
body: <p>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?).</p>
  
  <pre><code>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");
  </code></pre>
  
  <p>This is the exception I get, I googled without finding a solution.</p>
  
  <pre><code>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
  </code></pre>
  
  <p><a href="http://pastie.org/9741284" rel="nofollow">Watchdog.java source code</a></p>
  
  <p>Relevant gradle configuration:</p>
  
  <pre><code>android {
      compileSdkVersion 21
      buildToolsVersion '21.0.2'
  
      defaultConfig {
          minSdkVersion 19
          targetSdkVersion 21
  </code></pre>