Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to shutdown an Android mobile programmatically?

Tags:

android

Is it possible to shutdown the mobile programmatically. that is with out using su commands..

like image 849
Venkat Avatar asked May 02 '12 10:05

Venkat


2 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]

like image 73
Sheharyar Avatar answered Sep 20 '22 23:09

Sheharyar


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

like image 23
Krishnakant Dalal Avatar answered Sep 23 '22 23:09

Krishnakant Dalal