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.
getRuntime(). exec(new String[] { "su", "-c", "reboot" }); proc = Runtime. getRuntime(). exec(new String[]{"/system/bin/su","-c","reboot now"}); proc.
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.
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);
On API 24 if your app is the device owner app you can call: devicePolicyManager.reboot(yourAdminComponent)
See docs here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With