Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turn on/off Android device

Tags:

android

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?

like image 651
Nguyen Avatar asked Dec 13 '12 02:12

Nguyen


People also ask

How do I turn on my Android device?

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.

How do I turn this phone on or off?

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.


1 Answers

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.

like image 87
cjds Avatar answered Oct 12 '22 02:10

cjds