How to Reboot phone programmatically?

前端 未结 5 1419
情深已故
情深已故 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:06
    Process proc = null;
        try {
            proc = Runtime.getRuntime().exec(new String[] { "su", "-c", "reboot" });
            try {
                proc.waitFor();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-01-17 05:24

    As per the answer of @Maňish Yadav your app should be device owner to do rebooting due to security reasons.

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

    Programmatically switching off Android phone

    You need the system key to sign your app. See this post for details;

    How to compile Android Application with system permissions


    If you're root on your device, you can follow this method to become device owner.

    First, create a file device_owner.xml with following content:

    <?xml version="1.0" encoding="utf-8" standalone="yes" ?>
    <device-owner package="your.owner.app.package.id" name="Your app name" />
    

    Now do the following steps

    • adb push device_owner.xml /sdcard/

    • adb shell

    • su

    • cp /sdcard/device_owner.xml /data/system/

    • cd /data/system/

    • chown system:system device_owner.xml

    • reboot

    Note : Before rebooting device, make sure that you installed the application, which you are trying to make device owner. If you will not do, you will get boot animation for infinite time.

    For more info

    0 讨论(0)
  • 2021-01-17 05:26

    if you running Android M (API 23) you have to set runtime permission for reboot.

    If the device is running Android 6.0 or higher, and your app's target SDK is 23 or higher: The app has to list the permissions in the manifest, and it must request each dangerous permission it needs while the app is running. The user can grant or deny each permission, and the app can continue to run with limited capabilities even if the user denies a permission request.

    Requesting Permissions at Run Time

    0 讨论(0)
  • 2021-01-17 05:30

    This is my answer and code that might help you:

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.REBOOT)
                    != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.REBOOT}, 1);
                if(ContextCompat.checkSelfPermission(this, Manifest.permission.REBOOT)
                        != PackageManager.PERMISSION_GRANTED) {
                    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
                    pm.reboot(null);
                }
            } else {
                PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
                pm.reboot(null);
            }
    
    0 讨论(0)
提交回复
热议问题