在我的
Android应用程序中,我想在按钮单击时重新启动我的Android设备.
但它不起作用.
到目前为止我已经这样做了.
但它不起作用.
到目前为止我已经这样做了.
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","reboot now"}); proc.waitFor(); } catch (Exception ex) { Log.i(TAG,"Could not reboot",ex); } } });
<permission android:name="android.permission.REBOOT"/>
当我单击图像按钮重新启动时,我得到以下异常.
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
我的代码出了什么问题?请帮帮我.
对于信息,我正在测试android Lollipop(如果它很重要).
提前致谢.
解决方法
您需要的权限与重启方法无关,因为您的方法需要带有root权限的手机(带su).要重新启动手机,请按照您的要求获得权限,但请拨打
PowerManager#reboot
.
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); pm.reboot(null);