How could I power off an android device via java code?
Is it even possible? Do I need special permissions to do this?
Asked
Active
Viewed 8,933 times
8
Janusz
- 187,060
- 113
- 301
- 369
Johnny Mast
- 703
- 1
- 9
- 17
2 Answers
6
There is no API in the Android SDK to allow user apps to do this.
The closest you could perhaps try would be PowerManager.goToSleep().
Christopher Orr
- 110,418
- 27
- 198
- 193
-
I know there is this one app called "Power off" but i cant find the source code for that one :( – Johnny Mast Jan 10 '10 at 19:20
-
I think you'd need permission `DEVICE_POWER` which is system only. – Mirko N. Jan 10 '10 at 19:29
-
1There are apps on the Market that use internal, non-public APIs, or require your phone to be rooted. It's not an advisable approach. – Christopher Orr Jan 10 '10 at 19:31
2
Yes, It is possible but you need a Rooted device with Superuser Access. Try using the following code:
try {
Process proc = Runtime.getRuntime()
.exec(new String[]{ "su", "-c", "reboot -p" });
proc.waitFor();
} catch (Exception ex) {
ex.printStackTrace();
}
If you plan on doing this without Root Privileges, forget it. The only way to do that is to use PowerManager but that won't work unless your app is signed with the System Firmware Key.