Nguyen Nguyen - 3 months ago 20
Android Question

Turn on/off Android device

I want to implement some code lines that allow Android turn on/off screen like we quick turn on/off device by using Power button. Is there any way to do that?

Answer

It seems you want to lock the device not turn it off. To lock the device its a simple code. Mentioned here

It uses device Policy Manager lockNow() method

In case of a RuntimeError use the method described in this question to set permissions

Android DevicePolicyManager lockNow() problem

http://developer.android.com/reference/android/app/admin/DevicePolicyManager.html

To switch off the device:

This is not possible but you can reboot it using PowerManager

http://developer.android.com/reference/android/os/PowerManager.html#reboot

Note this requires REBOOT permission and OS may cancel it.

Comments