简体   繁体   中英

Android: shutting down the phone programmatically

Is it possible to shut down the whole phone by an app? How? Do I need to root the phone?

You need the DEVICE_POWER permission in order to shut the phone completely off which requires the device being rooted.

You can use the PowerManager to get it to sleep or reboot.

http://developer.android.com/reference/android/os/PowerManager.html#reboot(java.lang.String )

Reboot also requires a permission:

http://developer.android.com/reference/android/Manifest.permission.html#REBOOT

1、you device need root

2、code like this

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM