How to Reboot phone programmatically?

前端 未结 5 1420
情深已故
情深已故 2021-01-17 04:27

I am creating app(only for Android 6) in which I have to give reboot functionality using button click. I am using following code:

PowerManager pm =(PowerMan         


        
5条回答
  •  走了就别回头了
    2021-01-17 05:18

    Please try

    try {
    Runtime.getRuntime().exec("su");
      Runtime.getRuntime().exec("reboot");
    } catch (IOException e) {
    }  
    

    OR

    Runtime.getRuntime().exec(new String[]{"/system/bin/su","-c","reboot now"});
    

    if that not work : Runtime.getRuntime().exec(new String[]{"su","-c","reboot now"}); instead


    Update1

    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)

    Reboot the device. Will not return if the reboot is successful.Requires the REBOOT permission.

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

    Required to be able to reboot the device. Not for use by third-party applications. Constant Value: "android.permission.REBOOT"


    You cannot do this from an ordinary SDK application. Only applications signed with the system firmware signing key can do this.

    You need to sign your app with the System Firmware Key. But it's possible for your app with Root privileges. Try using the following code (if you have SU access):

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


    These do not work on Stock HTC ROMs, but haven't confirmed myself

提交回复
热议问题