19

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.

Phantômaxx
  • 36,442
  • 21
  • 78
  • 108
Suniel
  • 1,369
  • 7
  • 22
  • 39

9 Answers9

22

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);
MByD
  • 129,681
  • 25
  • 254
  • 263
  • 1
    I got this exception: java.lang.SecurityException: Neither user 10695 nor current process has android.permission.REBOOT. – Suniel Oct 07 '15 at 06:25
  • 7
    It seems that the permission only takes effect if your application is a system application, take a look here: https://stackoverflow.com/questions/3635101/how-to-sign-android-app-with-system-signature – MByD Oct 07 '15 at 06:37
  • This is not working in my case either and getting same exceptiojn as @Suniel gets. – Abdul Waheed Oct 02 '19 at 17:27
  • 1
    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: https://github.com/aosp-mirror/platform_frameworks_base/blob/8cbdf944528e513293b1e5e738b2c16a525f9520/core/res/AndroidManifest.xml#L3785. 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. – DADi590 Jan 11 '20 at 16:16
13

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

See docs here

Luke Cauthen
  • 640
  • 8
  • 19
  • 3
    what is yourAdminComponent? how to add admin component? – AAA May 13 '18 at 10:03
  • @AAA 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. – Stephen M -on strike- Aug 10 '18 at 22:05
4

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

sm_
  • 2,896
  • 2
  • 10
  • 21
  • Is this dependent on android version? Actually I am testing in android 5.0.1 – Suniel Oct 07 '15 at 06:36
  • 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. – mboy Dec 10 '16 at 05:00
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

Community
  • 1
  • 1
KishuDroid
  • 5,858
  • 4
  • 28
  • 45
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.

Yogesh Rathi
  • 5,684
  • 4
  • 42
  • 71
1

You can use the legendary LibSuperUser library.

Write this simple code:

Shell.SU.run("reboot");
Lennoard Silva
  • 466
  • 1
  • 5
  • 15
0

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

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

tylerr147
  • 1,883
  • 2
  • 8
  • 16
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()
        }
0

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");
      }
    }
  • 1
    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. – Hoppeduppeanut Mar 01 '21 at 05:42
  • hey @Hoppeduppeanut i will add a short explanation soon as possible – Aarush Kumar Mar 01 '21 at 10:01
  • @Hoppeduppeanut please see if it is correct now? – Aarush Kumar Mar 02 '21 at 05:16