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?
To enable the feature, open up the Settings window, scroll down and tap the Google entry. In the resulting window, scroll down, and tap Security. Tap Find My Device, and then tap the slider until it is in the On position. That's all there is to it.
Android smartphones and tablets Press and hold the phone's power button (generally on the top or right side of the device) for 1-2 seconds until the power options menu appears, then release the power button. Tap Restart or Power off on the menu.
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()
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With