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:)
Schedule Turn on/off android device programmatically on specific times
this is simply not possible, once you turn it off you lose control over the device.
Related Topics
Android Get Free Size of Internal/External Memory
How to Disable Copy/Paste From/To Edittext
How to Refresh App Upon Shaking the Device
How to Disable Bottomnavigationview Shift Mode
How to Include *.So Library in Android Studio
Where Is Debug.Keystore in Android Studio
How to Get the Resource Id of an Image If I Know Its Name
Manifest Merger Failed:Uses-Sdk:Minsdkversion 14
How to Keep Onitemselected from Firing Off on a Newly Instantiated Spinner
Lock the Android Device Programmatically
How to Add a Jar in External Libraries in Android Studio
Error Inflating Class Fragment
Percentage Width in a Relativelayout
How to Lay Out Views in Relativelayout Programmatically
Soft Keyboard Open and Close Listener in an Activity in Android
Android - How to Disable the Click of Home Button
Android Edittext Delete(Backspace) Key Event
Disabling the Fullscreen Editing View for Soft Keyboard Input in Landscape