Nguyen Nguyen - 1 year ago 77
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 Source

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

To switch off the device:

This is not possible but you can reboot it using PowerManager

Note this requires REBOOT permission and OS may cancel it.