Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Rebooting the Device

Author: Ian Darwin -- Published? false -- FormatLanguage: W

Problem:

For some strange reason, you want the user's Android device to reboot.

Solution:

  • Ask for REBOOT permission in your AndroidManifest.
  • Get a PowerManager instance from getSystemService(), and call its reboot method.

Discussion:

Since rebooting the operating system of the mobile device (phone or tablet) is so drastic, you must ask for it in your AndroidManifest.xml:

<uses-permission android:name="android.permission.REBOOT"/>

Even that will probably not be enough, however. You should be aware that the REBOOT permission has its protectionLevel parameter set to signatureOrSystem, meaning that it is only intended for use by (e.g, the application must be signed by) the organization that is creating the Android System Image. So unless you are using a custom-built system image, you will not be able to use this function!

Then, if you're really sure, go ahead and reboot:

PowerManager pwrMgr = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);

pwrMgr.reboot(null);

Again, if the application attempting the reboot is not signed with the same certificate as the system image, the permission in the manifest will be silently ignored, and you will get an Application Stopped Unexpectedly dialog.

The argument to the reboot call is named "reason", but the values are a few that are specially known to the kernel; this is not intended for logging the shutdown reason. Use the normal logging API for that.

Remember, just because you can do something does not mean that you should! There are very few circumstances where this is likely to be "socially acceptable". Hopefully most users will not capriciously install apps with this permission without a good reason...

See Also:

[1]

idarwin 2011-08-06 13:55:57.56 Thanks, Saket, I've updated the recipe to note that it can only be used by approved apps.
Saket 2011-08-05 14:24:54.674 Isn't the protectionLevel parameter set to signatureOrsystem?