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.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Sri Sri
  • 3,107
  • 7
  • 34
  • 37
  • 1
    Check my answer: http://stackoverflow.com/questions/2927083/how-to-realize-the-shutdown-using-code/8759117#8759117 It is possible with a rooted phone. – Rob Jan 06 '12 at 14:14

6 Answers6

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]

Community
  • 1
  • 1
Sheharyar
  • 73,588
  • 21
  • 168
  • 215
  • my phone is rooted, I push app in /system/app/ . But its not working for me. Is there any special in 4.0 ??? – NovusMobile Apr 16 '13 at 11:55
  • Does the SU dialog come up? Which phone, android & rom are you using? – Sheharyar Apr 16 '13 at 22:32
  • 1
    In my case this code works well when it executes in separate thread (otherwise su dialog is started but not visible). Thank you! – ruX Jul 28 '13 at 10:04
  • 1
    This one works for me for both cases... SHUTDOWN:: reboot -p .... REBOOT: reboot ... thanks for help :) – Noman Jul 01 '14 at 09:09
  • @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? – Basher51 Dec 31 '14 at 06:20
  • 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 `doInBackground` – Sheharyar Dec 31 '14 at 07:46
  • its working with "su". can it be possible without "su" command? – Sathish Aug 28 '15 at 06:20
  • @Sathish No. You can't shutdown without root. – Sheharyar Aug 28 '15 at 16:20
23

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

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    do you know any firmware that provides that functionality? Could you please recommend any? thanks. – Budda Jun 11 '12 at 23:26
  • 2
    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...) – Michael Apr 28 '14 at 21:24
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?

xil3
  • 16,305
  • 8
  • 63
  • 97
  • 17
    You cannot do a reboot from an ordinary SDK application. Only applications signed with the system firmware signing key can do this. – CommonsWare Sep 19 '10 at 12:08
  • 1
    Ah ok, fair enough - well I'll leave this up anyways so that he can read up about the PowerManager... – xil3 Sep 19 '10 at 12:15
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.

Nova Entropy
  • 5,727
  • 1
  • 19
  • 32
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.

Rupert Rawnsley
  • 2,622
  • 1
  • 29
  • 40
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.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Rich
  • 4,157
  • 5
  • 33
  • 45
  • This just turns the screen completely off for phones I've used it on. Must be a weird bug on your Tab – stealthcopter Nov 21 '12 at 15:19
  • 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. – Rich Nov 29 '12 at 13:55