Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
342 views
in Technique[技术] by (71.8m points)

shutdown - 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.

question from:https://stackoverflow.com/questions/3745523/programmatically-switching-off-android-phone

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...