Is it possible to shutdown the mobile programmatically. that is with out using su commands..
7 Answers
It is possible, but you need a Rooted
Android device with Superuser
access. You can't do it without Root unless your app is signed with the System Firmware Key
. Try using the following code:
Shutdown:
try {
Process proc = Runtime.getRuntime()
.exec(new String[]{ "su", "-c", "reboot -p" });
proc.waitFor();
} catch (Exception ex) {
ex.printStackTrace();
}
Reboot:
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]

- 73,588
- 21
- 168
- 215
-
1
-
3Are you sure you used "reboot -p" and not only "reboot". Also please tell us your device, android version and rom. – Sheharyar Jun 29 '13 at 16:23
-
@Sheharyar is it possible to disable the shut down/silent programmatically? when device is locked or unlocked ?? – rupesh Sep 03 '15 at 06:51
-
-
Is your device rooted properly and have you tested it with other `su` commands? – Sheharyar Sep 14 '17 at 20:27
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

- 3,568
- 7
- 34
- 62
-
-
@Krishnakant Dalal is it possible to disable the shut down/silent programmatically? when device is locked or unlocked ?? – rupesh Sep 03 '15 at 06:51
This is the code i use to perform any system command.
void shutdown_sys()
{
Process chperm;
try {
chperm=Runtime.getRuntime().exec("su");
DataOutputStream os =
new DataOutputStream(chperm.getOutputStream());
os.writeBytes("shutdown\n");
os.flush();
chperm.waitFor();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Call this function from your android App. It will work if su is functional in your system. Let me know in case it does not work. I dont have an Android base ready to test. But the same works for reboot. So shutdown is also a linux shell command which ithink will be there in Android as well.. All the best

- 18,356
- 16
- 68
- 108
-
Ok. reboot and shutdown, these commands are system commands. to tun these commands you need to be a root user of phone. By default you dont have root previlages. You need to hack your phone and root it. Just type in youtube or google on how to root your specific android model. You will surely get to know more. Rooting basically changes the OS previlages where you can change from user mode to root mode and vice-versa.. – Sandeep May 02 '12 at 10:31
-
you need to root your mobile phone. Just google out how to do this. You have xroot and some other tools which can be used to root the device. Just google on how to root your mobile phone.... this is what should be your first step – Sandeep May 02 '12 at 10:39
-
my req is when ever i click a button in the app it the mobile automatically shutdown.but if i want to execute the su cmds i change the mobile settings mannual.. so i leave it... – Venkat May 03 '12 at 09:16
-
-
you can call the above function on a click. But as stated earlier it is a previleged command and can execute only after su from application – Sandeep May 03 '12 at 09:19
-
let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/10830/discussion-between-venkat-and-happy2help) – Venkat May 03 '12 at 09:20
-
-
@mk: Using your snippet abruptly shutsdown the device.Its not a clean shutdown.Is that how its supposed to work or am I missing anything? – Basher51 Dec 31 '14 at 06:30
-
-
hey, what about if we dont have root privileges? am i just capable of rebooting (restarting) only? – gumuruh Apr 25 '22 at 02:01
With rooted shell command type in, svc power shutdown works for me in android 7.1

- 21
- 1
-
Work for me, I have a system app installed in app-priv. See below – Jonathan Le Cornichone Aug 25 '22 at 14:21
I don't see any resemblance with your title and the code snippet in the body. The snippet is related with KeyGuardManager which is used for lock and unlock the key pad.
In order to shutdown your phone you will need to use PowerManager or SU commands. (On rooted devices.)

- 20,548
- 30
- 97
- 138
If your device is rooted then you can try below solution
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"
.

- 6,331
- 4
- 51
- 81
-
works like a charm on motorola x4 rooted by magisk, using lineageos 16. – LucasRT Aug 18 '19 at 03:22
-
but what if i dont have a any rooted phone yet? Can i only restaring the phone? or .... something else that mimic shutting down? Such as making the memory full of loop until it crashed like a shutting down? – gumuruh Apr 25 '22 at 02:02
My solution : I have a system app installed in priv-app, no signed with the system key.
Process proc = Runtime.getRuntime().exec(new String[] { "/system/bin/svc", "power", "shutdown" });
proc.waitFor();

- 342
- 2
- 19