1

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 =(PowerManager) getSystemService(Context.POWER_SERVICE);   
pm.reboot(null);

and added permission:

uses-permission android:name="android.permission.REBOOT",    
uses-permission android:name="android.permission.DEVICE_POWER" ,    
uses-permission android:name="android.permission.MODIFY_PHONE_STATE"

but getting following error:

java.lang.SecurityException: Neither user 10298 nor current process has android.permission.REBOOT.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Pratik
  • 25
  • 1
  • 4
  • See http://stackoverflow.com/questions/4966486/reboot-the-phone-on-a-button-click – yrazlik Jan 04 '17 at 06:26
  • See this: http://stackoverflow.com/a/32984952/5241603 – K.Sopheak Jan 04 '17 at 06:27
  • 2
    `android.permission.REBOOT` is only granted to system application and not to any user application. But from API 24 i.e. Nougat, if your app is the device owner app you can call: `devicePolicyManager.reboot(yourAdminComponent)` [Doc](https://developer.android.com/reference/android/app/admin/DevicePolicyManager.html#reboot(android.content.ComponentName)) – Rajen Raiyarela Jan 04 '17 at 06:35

5 Answers5

2

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

Maveňツ
  • 1
  • 12
  • 50
  • 89
  • 1
    @Pratik did you added run time permissions for > Android M (API 23) ... I have updated my answer you can check now. – Maveňツ Jan 04 '17 at 06:41
  • i have added run time permissions too. but things are same. – Pratik Jan 04 '17 at 06:45
  • 1
    You need to sign your app with the System Firmware Key. But it's possible for your app with Root privileges – Maveňツ Jan 04 '17 at 06:47
  • @Pratik Are you still alive???? Do you getting any problem in understanding my code? – Maveňツ Jan 04 '17 at 07:45
  • I went through the suggested code. but getting same error. Does above code require rooted phone? – Pratik Jan 04 '17 at 12:09
  • 1
    did you tried all the above answers .. I have mentioned each and everything. – Maveňツ Jan 04 '17 at 12:10
  • @Pratik Are you running Android M? If so, this is because it's not enough to declare permissions in the manifest. For some permissions, you have to explicitly ask user in the [runtime](http://developer.android.com/training/permissions/requesting.html) . For more info check this [link](http://stackoverflow.com/questions/21183328/how-to-make-my-app-a-device-owner) – Maveňツ Jan 04 '17 at 12:13
  • Yes. I have asked for run time permission as shown above in question. – Pratik Jan 04 '17 at 12:15
  • @Pratik you doesn't seems to be legit at your point .. Would you like to show us the latest code you have now? – Maveňツ Jan 04 '17 at 12:17
1

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

sasikumar
  • 12,540
  • 3
  • 28
  • 48
0

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

Community
  • 1
  • 1
Maveňツ
  • 1
  • 12
  • 50
  • 89
0
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();
    }
Horrorgoogle
  • 7,858
  • 11
  • 48
  • 81
-3

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);
        }
PradyumanDixit
  • 2,372
  • 2
  • 12
  • 20