How to shutdown an Android mobile programmatically?

前端 未结 6 1417
慢半拍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]

    0 讨论(0)
  • 2020-11-28 13:31

    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

    0 讨论(0)
  • 2020-11-28 13:32

    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".

    0 讨论(0)
  • 2020-11-28 13:47

    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

    0 讨论(0)
  • 2020-11-28 13:47

    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.)

    0 讨论(0)
  • 2020-11-28 13:51

    With rooted shell command type in, svc power shutdown works for me in android 7.1

    0 讨论(0)
提交回复
热议问题