Programmatically switching off Android phone
Asked Answered
E

6

37

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.

Electrode answered 19/9, 2010 at 11:42 Comment(1)
Check my answer: #2927583 It is possible with a rooted phone.Expertism
G
16

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?

Guttle answered 19/9, 2010 at 11:54 Comment(2)
You cannot do a reboot from an ordinary SDK application. Only applications signed with the system firmware signing key can do this.Sessoms
Ah ok, fair enough - well I'll leave this up anyways so that he can read up about the PowerManager...Guttle
C
38

As CommonsWare already said that this is not possible in an Ordinary SDK Application. You need to sign your app with the System Firmware Key. But it's possible for your app with Root privileges. Try using the following code (if you have SU access):

Shutdown:

try {
    Process proc = Runtime.getRuntime()
                    .exec(new String[]{ "su", "-c", "reboot -p" });
    proc.waitFor();
} catch (Exception ex) {
    ex.printStackTrace();
}

Restart:

Same code, just use "reboot" instead of "reboot -p".


[On an other note: I read somewhere that these commands do not work on Stock HTC ROMs, but haven't confirmed myself]

Chess answered 14/9, 2012 at 19:12 Comment(8)
my phone is rooted, I push app in /system/app/ . But its not working for me. Is there any special in 4.0 ???Hyperventilation
Does the SU dialog come up? Which phone, android & rom are you using?Chess
In my case this code works well when it executes in separate thread (otherwise su dialog is started but not visible). Thank you!Rebel
This one works for me for both cases... SHUTDOWN:: reboot -p .... REBOOT: reboot ... thanks for help :)Splore
@Sheharyar: Usually when we shutdown the normal way we see a dialog showing 'Shuting down...',however when the above snippet is executed the tab shutdowns very abruptly.Is this how it should normally work/execute(without showing any dialog) prior to the shutdown?Reconcile
Yes, that happens because this does not properly end all running apps. But if you want to give the same effect try wrapping this in an AsyncTask with a ProgressBar and a few seconds of sleep in doInBackgroundChess
its working with "su". can it be possible without "su" command?Expel
@Expel No. You can't shutdown without root.Chess
S
23

You cannot do this from an ordinary SDK application. Only applications signed with the system firmware signing key can do this.

Sessoms answered 19/9, 2010 at 11:52 Comment(2)
do you know any firmware that provides that functionality? Could you please recommend any? thanks.Griffon
Actually you can. Just set up something that sends frequent pending intents (for example, GPS at 1Hz or something like that) and then never actually receives them. In some cases this will cause a spurious reboot in under a minute. (Not really a recommended way though...)Danille
G
16

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?

Guttle answered 19/9, 2010 at 11:54 Comment(2)
You cannot do a reboot from an ordinary SDK application. Only applications signed with the system firmware signing key can do this.Sessoms
Ah ok, fair enough - well I'll leave this up anyways so that he can read up about the PowerManager...Guttle
W
9

A much better solution is to run:

su -c am start -a android.intent.action.ACTION_REQUEST_SHUTDOWN

You can use a Process for this.

Worm answered 28/4, 2015 at 13:42 Comment(1)
great solution, except the command after su should be in quotes.Hike
D
6

If you have the sigOrSystem permission android.permission.SHUTDOWN you can raise the protected ACTION_REQUEST_SHUTDOWN Intent like this:

Intent intent = new Intent("android.intent.action.ACTION_REQUEST_SHUTDOWN");
intent.putExtra("android.intent.extra.KEY_CONFIRM", false);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

Note that prior to Android 4.0, this permission was signature only.

Declan answered 12/6, 2015 at 15:7 Comment(1)
This is not working in my case applicating crashed with No Activity found to handle IntentTomas
B
1

Actually, the above responses are not completely accurate, in my experience. I was experimenting with ways to dim the screen and found that the following:

Window w = getWindow();
WindowManager.LayoutParams lp = w.getAttributes();
lp.screenBrightness =.005f;
w.setAttributes (lp);

will actually turn my Samsung Galaxy Tab off if, instead of 0.005, I use a screen brightness value of 0.

I suspect this is bug somewhere, but I don't have sufficient hardware to test the code on other Android models. Hence, I can't really tell you what will happen on your phone. I can tell you that my code shuts of my phone even completely unsigned.

Bookbinding answered 23/4, 2012 at 19:18 Comment(2)
This just turns the screen completely off for phones I've used it on. Must be a weird bug on your TabQuinn
Which was actually the effect I was looking for at the time -- oh well. We've since replaced the phone for our system, AND determined that wasn't a function we wanted anyway, but it was a very interesting thing to discover.Bookbinding

© 2022 - 2024 — McMap. All rights reserved.