Turn off device programmatically
In my case, I do not think it is possible to shut the device down how I would like to.
The closest that I managed to get to my target was using:
Intent i = new Intent("android.intent.action.ACTION_REQUEST_SHUTDOWN");
i.putExtra("android.intent.extra.KEY_CONFIRM", true);
startActivity(i);
That brings up a dialog to turn the device off. This is the perfect solution, but in my case, using it causes the device to crash. It may be that my device is somewhat special, and other devices will not have these restrictions.
In any case, I hope that my testing will help others in their quest.
Programmatically switching off Android phone
You could possibly use the PowerManager to make it reboot (this does not guarantee that it'll reboot - OS may cancel it):
http://developer.android.com/reference/android/os/PowerManager.html#reboot(java.lang.String)
It requires the REBOOT permission:
http://developer.android.com/reference/android/Manifest.permission.html#REBOOT
Can you also check your logcat when trying to enable/disable keyguard, and post what's there?
How to shutdown an Android mobile programmatically?
You could possibly use the PowerManager to make it reboot (this does not guarantee that it'll reboot - OS may cancel it):
http://developer.android.com/reference/android/os/PowerManager.html#reboot(java.lang.String)
It requires the REBOOT permission:
http://developer.android.com/reference/android/Manifest.permission.html#REBOOT
Can you also check your logcat when trying to enable/disable keyguard, and post what's there?
You cannot do this from an ordinary SDK application. Only applications signed with the system firmware signing key can do this
How to shut down Android phone, programmatically?
Even if your phone is rooted, when apps that require root are accessing these system areas it will prompt for permission from the user atleast once(1st time of using the app). Either you can allow each time which will allow root permissions for your app for specified time limit(usually 15 mins) or you can select the "Remember my choice" and allow permanent access to your app this way.
In case you allowed once with the "Remember my choice" then you will have to manually change this permission from your super user app.
NOTE: Once if your device is rooted, you will get an app named "SuperSU" or "Superuser" in your device which will display all the apps that requires ROOT permission and the default access level of these apps.
Hope this helps:)
Related Topics
Android Listview Setselection() Does Not Seem to Work
Android Studio: Unable to Start the Daemon Process
Set Font for All Textviews in Activity
Accessing Files from Genymotion Sd Card
Adt Blank Activity Created with Fragment Activity
Java.Io.Ioexception: Hostname Was Not Verified
Whatsapp Like Collapsing Toolbar
Can Android's Webview Automatically Resize Huge Images
Android - Build a Notification, Taskstackbuilder.Addparentstack Not Working
How to Automatically Uninstall Android App from Device Before Installing a New Version
Add Elevation/Shadow on Toolbar for Pre-Lollipop Devices
How to Execute Something Just Once Per Application Start
Convert Xml to JSON Object in Android
Smoothscrolltopositionfromtop() Is Not Always Working Like It Should
Video Streaming Using Rtsp: Android
Pass List of Objects from One Activity to Other Activity in Android