Search code examples
androidshutdown

Programmatically switching off Android phone


Can we switch off an Android phone programmatically?

I am using following snippet but it didn't work for me.

KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Activity.KEYGUARD_SERVICE); 
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE); 

lock.disableKeyguard(); // to disable

lock.reenableKeygaurd();// to enable

and I used the permission also.


Solution

  • 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?