Rebooting the Device
In Chapter: System and Device Control
Author: Ian Darwin ('idarwin')
For some strange reason, you want the user's Android device to reboot.
- Ask for REBOOT permission in your AndroidManifest.
- Get a PowerManager instance from getSystemService(), and call its reboot method.
Since rebooting the operating system of the mobile device (phone or tablet) is so drastic, you must ask for it in your AndroidManifest.xml:
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);
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...
If you found this recipe useful, why not
buy the book
and have the whole collection always at hand?"