出于某种原因,我无法使用runtime.getruntime().exec(“/system/bin/reboot”);重新启动Android设备。我已经在3台设备上尝试了下面的代码,但没有成功。其中一个是从rowboat android源代码构建的。另外两个是摩托罗拉Droid Pro(根,股票)和HTC Ledgent(根,cynogen mod)。所有设备都运行android 2.2froyo。
有人知道为什么吗?su工作得很好,超级用户应用程序也是可见的。我应该注意到其他的shell命令也可以工作,比如netcfg(chmod'to 777)和ls。
public static boolean executeCommand(SHELL_CMD iShellCmd){
String cmd = null;
String line = null;
String fullResponse = null;
Process lPs = null;
Log.d(Global.TAG,"--> !!!!! Running shell command");
if (iShellCmd == SHELL_CMD.restart_device){
cmd = "reboot";
}
Log.d(Global.TAG,"--> About to exec: " + cmd);
try {
lPs = Runtime.getRuntime().exec("su");
lPs = Runtime.getRuntime().exec("/system/bin/reboot");
} catch (Exception e) {
e.printStackTrace();
}
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(lPs.getOutputStream()));
BufferedReader in = new BufferedReader(new InputStreamReader(lPs.getInputStream()));
try {
while ((line = in.readLine()) != null) {
Log.d(Global.TAG,"--> Command result line: " + line);
if (fullResponse == null){
fullResponse = line;
}else{
fullResponse = fullResponse + "\n" + line;
}
}
} catch (Exception e) {
e.printStackTrace();
}
Log.d(Global.TAG,"--> Full response was: " + fullResponse);
return true;
}
最佳答案
根据您在设备上获得根权限的方式,您可以执行以下任一操作:
Runtime.getRuntime().exec(new String[]{"/system/xbin/su","-c","reboot"});
或
Runtime.getRuntime().exec(new String[]{"/system/bin/su","-c","reboot"});
或
Runtime.getRuntime().exec(new String[]{"su","-c","reboot"});
最好在应用程序中测试这三个场景。
关于android - 无法使用runtime.exec重新启动设备,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5603221/