Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restarting a device programmatically

Tags:

android

reboot

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.

like image 380
Suniel Avatar asked Oct 07 '15 06:10

Suniel


People also ask

How to restart android device programmatically?

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

How do you restart your Android phone?

Android smartphones and tabletsPress and hold the phone's power button (generally on the top or right side of the device) for 1-2 seconds until the power options menu appears, then release the power button. Tap Restart or Power off on the menu.


2 Answers

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);
like image 177
MByD Avatar answered Oct 18 '22 14:10

MByD


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

See docs here

like image 21
Luke Cauthen Avatar answered Oct 18 '22 15:10

Luke Cauthen