Restarting a device programmatically
Asked Answered
P

10

22

In my android application, I want to restart my android device on button click. But its not working.
I have done this so far.

ImageButton restartmob = (ImageButton) this.findViewById(R.id.restartmob);
    restartmob.setOnClickListener(new ImageButton.OnClickListener() {
        @Override
        public void onClick(View v) {
            Process proc = null;            
            try {
               //proc = Runtime.getRuntime().exec(new String[] { "su", "-c", "reboot" });
                 proc = Runtime.getRuntime().exec(new String[]{"/system/bin/su","-c","reboot now"});
                proc.waitFor();
            } catch (Exception ex) {
                Log.i(TAG, "Could not reboot", ex);
            }
        }
    });

Also I put the following permission in the manifest file.

<permission android:name="android.permission.REBOOT"/>

When I clicked the image button to restart, I got the following exception.

java.io.IOException: Error running exec(). Command: [/system/bin/su, -c, reboot now] 
Working Directory: null Environment: null   
at java.lang.ProcessManager.exec(ProcessManager.java:211)   
at java.lang.Runtime.exec(Runtime.java:173)
at java.lang.Runtime.exec(Runtime.java:128)
at com.android.onlinepayment.activity.SystemSubMenuActivity$1.onClick(SystemSubMenuActivity.java:49)
at android.view.View.performClick(View.java:5184)   
at android.view.View$PerformClick.run(View.java:20910)  
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)    
at java.lang.reflect.Method.invoke(Native Method)   
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388  
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
Caused by: java.io.IOException: No such file or directory   
at java.lang.ProcessManager.exec(Native Method)
at java.lang.ProcessManager.exec(ProcessManager.java:209)
... 13 more

What's wrong with my code? Please help me on this.
For the info, I am testing on android Lollipop (if it matters).
Thanks in Advance.

Phelps answered 7/10, 2015 at 6:3 Comment(1)
See this answer: #4966986Disheveled
R
26

The permission you required is not related to your reboot method, as your method requires a rooted phone (with su). To reboot the phone, require the permission as you did, but call PowerManager#reboot.

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
pm.reboot(null);
Rosenfeld answered 7/10, 2015 at 6:12 Comment(4)
I got this exception: java.lang.SecurityException: Neither user 10695 nor current process has android.permission.REBOOT.Phelps
It seems that the permission only takes effect if your application is a system application, take a look here: #3635601Rosenfeld
This is not working in my case either and getting same exceptiojn as @Phelps gets.Postmillennialism
What if I have a rooted phone, app with root access, and installed as system and it gives the same error as Suniel? (java.lang.SecurityException: Neither user [don't remember] nor current process has android.permission.REBOOT). It's said here that the protectionLevel is signature|privileged: github.com/aosp-mirror/platform_frameworks_base/blob/…. I'd be the last one, supposedly. Also I'm doing this on Lollipop 5.1, no it shouldn't be the Mashmallow runtime permission request.Sepulcher
F
18

On API 24 if your app is the device owner app you can call: devicePolicyManager.reboot(yourAdminComponent)

See docs here

Fallacy answered 10/8, 2016 at 16:57 Comment(2)
what is yourAdminComponent? how to add admin component?Prosector
@Prosector the AdminComponent is a ComponentName that is set when you become device owner. Looks something like "com.mycompany.product/.DeviceOwner", which references a class name.Collarbone
N
5

Try this...

try {
      Process proc = Runtime.getRuntime().exec(new String[] { "su", "-c", "reboot" });
      proc.waitFor();
         } catch (Exception ex) {
                Log.i(TAG, "Could not reboot", ex);
      }

Add permission reboot

Nolte answered 7/10, 2015 at 6:12 Comment(3)
Is this dependent on android version? Actually I am testing in android 5.0.1Phelps
No need to add permission reboot because you cannot get this permission in your app just use any root permission manager then allow your app by default for root access.Banna
This is the only working solution for Android 7 with Magisk. Specifying full path by "/system/bin/su" is not working because "/system/bin/su" is not exist by this path.Squad
S
2

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

Sender answered 7/10, 2015 at 6:16 Comment(0)
C
2

After long struggle i found working solution.

If your system is used serial port then execute below command,

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

if use normal port then excure below command

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

difference is only /bin and /xbin

so can code like if first command throw exception then execute second.

Crosspollination answered 11/7, 2016 at 5:19 Comment(1)
This is not working in my case. it is giving me exception no such directory or file found. Am I missing anything?Postmillennialism
T
1

It is only possible with root access. You can use the LibSuperUser library or libsu.

Shell.SU.run("reboot");
Toscanini answered 27/8, 2016 at 12:56 Comment(0)
S
1

try this code -

First importing package

import com.google.android.things.device.DeviceManager;

Creating function and adding some Exceptions

public void StartReboot() throws IllegalStateException, RuntimeException, SecurityException {

Creating Object

      try {
         DeviceManager deviceManager = DeviceManager.getInstance();

Start Reboot

         deviceManager.reboot();

Catching Exceptions

      } catch(IllegalStateException ex) {
        OnError("Underlying system service is not ready.");
      } catch(RuntimeException ex) {
        OnError("Underlying system service encountered an error.");
      } catch(SecurityException ex) {
        OnError("Calling context does not have com.google.android.things.permission.REBOOT permission");
      }
    }
Sundry answered 1/3, 2021 at 5:9 Comment(3)
While this might answer the question, if possible you should edit your answer to include a short explanation of how this code block answers the question. This helps to provide context and makes your answer much more useful for future readers.Stripe
hey @Stripe i will add a short explanation soon as possibleSundry
@Stripe please see if it is correct now?Sundry
S
0

You are doing <permission ... />. You need to do <USES-permission ... />

The caps are not needed, it was just to emphasise it.

Sholapur answered 8/10, 2016 at 3:14 Comment(0)
R
0
try {//   It worked for me on Android 7.0
            Runtime.getRuntime().exec(arrayOf("/system/bin/su", "-c", "reboot now"))
        }catch (e:Exception){
            Log.e(TAG,e.message)
            e.printStackTrace()
        }
Rhondarhondda answered 7/11, 2019 at 17:15 Comment(0)
S
0

Other answers didn't consider DevicePolicyManager API. Perhaps you can set your app as Device Admin http://blogs.quovantis.com/android-device-administration-apis/ (user needs to do it specifically from settings) and there is a reboot method https://developer.android.com/reference/android/app/admin/DevicePolicyManager#reboot(android.content.ComponentName).

So I doubt root is needed for sure to do it. I'm writing the app to test it (trying to make remote reboot from computer possible).

Sparrowgrass answered 22/9, 2021 at 21:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.