title: Power-Off or Restart an android device via Code/Program?
body: <p>Is there any way to restart/shutdown a phone directly from an application?</p>
  
  <p>For Example:
  I need to restart/shutdown my Phone when i meet some specific condition...</p>
  
  <p><strong>Citations to Developer Website:</strong></p>
  
  <p>Permission to Reboot?</p>
  
  <p><a href="http://developer.android.com/reference/android/Manifest.permission.html#REBOOT">http://developer.android.com/reference/android/Manifest.permission.html#REBOOT</a></p>
  
  <p>Permission to Brick the device??? </p>
  
  <p><a href="http://developer.android.com/reference/android/Manifest.permission.html#BRICK">http://developer.android.com/reference/android/Manifest.permission.html#BRICK</a></p>
  
  <p>Method to reboot???</p>
  
  <p><a href="http://developer.android.com/reference/android/os/PowerManager.html#reboot%28java.lang.String%29">http://developer.android.com/reference/android/os/PowerManager.html#reboot%28java.lang.String%29</a></p>
  
  <p>Method to reboot and Wipe?? </p>
  
  <p><a href="http://developer.android.com/reference/android/os/RecoverySystem.html#rebootWipeUserData%28android.content.Context%29">http://developer.android.com/reference/android/os/RecoverySystem.html#rebootWipeUserData%28android.content.Context%29</a></p>
  
  <p>Reboot Method in MonkeyRunner/MonkeyDevice:</p>
  
  <p><a href="http://developer.android.com/guide/developing/tools/MonkeyDevice.html#reboot">http://developer.android.com/guide/developing/tools/MonkeyDevice.html#reboot</a></p>
  
  <p>There are options to brick a device but why not to power-down or restart?</p>
  
  <p>I tried the following code but it throws an exception..</p>
  
  <p><strong>Manifest.xml:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.schogini.PowerOff"
        android:versionCode="1"
        android:versionName="1.0"&gt;
  
  
      &lt;uses-permission android:name="android.permission.REBOOT" /&gt;
      &lt;uses-permission android:name="android.permission.MODIFY_PHONE_STATE" /&gt;
      &lt;uses-permission android:name="android.permission.DEVICE_POWER" /&gt;
  
      &lt;application android:icon="@drawable/icon" android:label="@string/app_name"&gt;
          &lt;activity android:name=".PowerOffActivity"
                    android:label="@string/app_name"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
  
      &lt;/application&gt;
  &lt;/manifest&gt;
  </code></pre>
  
  <p><strong>Java Code:</strong></p>
  
  <pre><code>package com.schogini.PowerOff;
  
  
  import android.app.Activity;
  import android.content.Context;
  import android.os.Bundle;
  import android.os.PowerManager;
  import android.view.View;
  import android.widget.Button;
  
  
  public class PowerOffActivity extends Activity {
  
      PowerManager pm;
  
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
          Button mBuyButton = (Button) findViewById(R.id.button1);
  
          mBuyButton.setOnClickListener(new View.OnClickListener() 
          {
              public void onClick(View v) 
              {
                  pm.reboot("null");
              }
          });
      }
  }
  </code></pre>
  
  <p><strong>Exception thrown in LogCat:</strong></p>
  
  <pre><code>06-10 17:58:29.001: WARN/dalvikvm(2064): threadid=3: thread exiting with uncaught exception (group=0x4001b160)
  06-10 17:58:29.001: ERROR/AndroidRuntime(2064): Uncaught handler: thread main exiting due to uncaught exception
  06-10 17:58:29.011: ERROR/AndroidRuntime(2064): java.lang.NoSuchMethodError: android.os.PowerManager.reboot
  06-10 17:58:29.011: ERROR/AndroidRuntime(2064):     at com.schogini.PowerOff.PowerOffActivity$1.onClick(PowerOffActivity.java:28)
  06-10 17:58:29.011: ERROR/AndroidRuntime(2064):     at android.view.View.performClick(View.java:2364)
  06-10 17:58:29.011: ERROR/AndroidRuntime(2064):     at android.view.View.onTouchEvent(View.java:4179)
  06-10 17:58:29.011: ERROR/AndroidRuntime(2064):     at android.widget.TextView.onTouchEvent(TextView.java:6545)
  06-10 17:58:29.011: ERROR/AndroidRuntime(2064):     at android.view.View.dispatchTouchEvent(View.java:3709)
  06-10 17:58:29.011: ERROR/AndroidRuntime(2064):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
  06-10 17:58:29.011: ERROR/AndroidRuntime(2064):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
  06-10 17:58:29.011: ERROR/AndroidRuntime(2064):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
  06-10 17:58:29.011: ERROR/AndroidRuntime(2064):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
  06-10 17:58:29.011: ERROR/AndroidRuntime(2064):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
  06-10 17:58:29.011: ERROR/AndroidRuntime(2064):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
  06-10 17:58:29.011: ERROR/AndroidRuntime(2064):     at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
  06-10 17:58:29.011: ERROR/AndroidRuntime(2064):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
  06-10 17:58:29.011: ERROR/AndroidRuntime(2064):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
  06-10 17:58:29.011: ERROR/AndroidRuntime(2064):     at android.os.Handler.dispatchMessage(Handler.java:99)
  06-10 17:58:29.011: ERROR/AndroidRuntime(2064):     at android.os.Looper.loop(Looper.java:123)
  06-10 17:58:29.011: ERROR/AndroidRuntime(2064):     at android.app.ActivityThread.main(ActivityThread.java:4363)
  06-10 17:58:29.011: ERROR/AndroidRuntime(2064):     at java.lang.reflect.Method.invokeNative(Native Method)
  06-10 17:58:29.011: ERROR/AndroidRuntime(2064):     at java.lang.reflect.Method.invoke(Method.java:521)
  06-10 17:58:29.011: ERROR/AndroidRuntime(2064):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
  06-10 17:58:29.011: ERROR/AndroidRuntime(2064):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
  06-10 17:58:29.011: ERROR/AndroidRuntime(2064):     at dalvik.system.NativeStart.main(Native Method)
  </code></pre>