在我的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","-c","reboot now"});
proc.waitFor();
} catch (Exception ex) {
Log.i(TAG, "Could not reboot", ex);
}
}
});
另外,我在清单文件中添加了以下权限。
当我单击图像按钮重新启动时,我得到了以下异常。
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棒棒糖(如果重要的话)。
提前谢谢。
发布于 2015-10-07 14:12:38
您所需的权限与您的重启方法无关,因为您的方法需要根电话(带有
)。要重新启动电话,需要像您一样获得权限,但调用
(
https://developer.android.com/reference/android/os/PowerManager.html#reboot(java.lang.String%29)
..。
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
pm.reboot(null);
发布于 2016-08-11 00:57:51
在API 24上,如果您的应用程序是设备所有者应用程序,则可以调用:
请参阅文档
这里
(
https://developer.android.com/reference/android/app/admin/DevicePolicyManager.html#reboot(android.content.ComponentName%29
)
发布于 2015-10-07 14:12:18
试试这个..。
try {
Process proc = Runtime.getRuntime().exec(new String[] { "su", "-c", "reboot" });
proc.waitFor();
} catch (Exception ex) {
Log.i(TAG, "Could not reboot", ex);
}
添加权限重新启动
https://stackoverflow.com/questions/32984849
复制相似问题