How to shutdown an Android mobile programmatically?

前端 未结 6 1441
慢半拍i
慢半拍i 2020-11-28 13:12

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

6条回答
  •  隐瞒了意图╮
    2020-11-28 13:31

    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]

提交回复
热议问题