在我的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棒棒糖(如果重要的话)。
提前谢谢。
发布于 2021-03-01 13:09:45
试试这个代码-
第一次导入包
import com.google.android.things.device.DeviceManager;
创建函数并添加一些异常
public void StartReboot() throws IllegalStateException, RuntimeException, SecurityException {
创建对象
try {
DeviceManager deviceManager = DeviceManager.getInstance();
启动重启
deviceManager.reboot();
捕获异常
} 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");
}
}
https://stackoverflow.com/questions/32984849
复制相似问题