Java Runtime.exec():是否在Android中重新启动?
我正在寻找一个解决方案,可以用来重新启动根设备。我知道重新启动设备对于用户来说是一个非常糟糕的设计,而不是真正的应用程序。主要目的是在测试期间重新启动手机(我使用视频聊天应用程序,有时当一切都不顺利时需要重新启动) 我观察到,在终端中使用reboot(例如,Java Runtime.exec():是否在Android中重新启动?,java,android,runtime,root,reboot,Java,Android,Runtime,Root,Reboot,我正在寻找一个解决方案,可以用来重新启动根设备。我知道重新启动设备对于用户来说是一个非常糟糕的设计,而不是真正的应用程序。主要目的是在测试期间重新启动手机(我使用视频聊天应用程序,有时当一切都不顺利时需要重新启动) 我观察到,在终端中使用reboot(例如,adb shell或ConnectBot)重新启动手机要比使用我无论如何都无法使用的常规方法重新启动快得多 目前,我可以通过 Process root = Runtime.getRuntime().exec("su"); 但是我不能重新启动
adb shell
或ConnectBot)重新启动手机要比使用我无论如何都无法使用的常规方法重新启动快得多
目前,我可以通过
Process root = Runtime.getRuntime().exec("su");
但是我不能重新启动。我试过G1(HTC)和Galaxy S(三星),但没有成功。我在/system/bin/reboot
以下是我的一些尝试:
Process reboot = Runtime.getRuntime().exec("/system/bin/reboot");
Process reboot = Runtime.getRuntime().exec("reboot");
Process reboot = Runtime.getRuntime().exec("su reboot");
我读到了Runtime.exec()的缺陷,但我想我不是在这种情况下
由于使用ConnectBot使我能够执行这样的操作,我非常确信这是可能的。请不要让我去看电影,这是一个大而复杂的项目:)
你能帮我解决这个问题吗
谢谢。重新启动在android中运行良好。您可能没有正确执行runtime.exec()。 你需要处理这个问题
public static void rebootSU() {
Runtime runtime = Runtime.getRuntime();
Process proc = null;
OutputStreamWriter osw = null;
StringBuilder sbstdOut = new StringBuilder();
StringBuilder sbstdErr = new StringBuilder();
String command="/system/bin/reboot";
try { // Run Script
proc = runtime.exec("su");
osw = new OutputStreamWriter(proc.getOutputStream());
osw.write(command);
osw.flush();
osw.close();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (osw != null) {
try {
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
try {
if (proc != null)
proc.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
sbstdOut.append(ReadBufferedReader(new InputStreamReader(proc
.getInputStream())));
sbstdErr.append(ReadBufferedReader(new InputStreamReader(proc
.getErrorStream())));
if (proc.exitValue() != 0) {
}
}
经过数周的搜索,终于:
Runtime.getRuntime().exec(new String[]{"/system/bin/su","-c","reboot now"});
我发现我无法按程序重新启动 此外,我可以使用终端模拟器应用程序在android手机上打开终端窗口, su型 获取根访问的#提示 然后键入“#重新启动” 我得到的回答是“不允许!” 有什么建议吗 好吧,没关系,我知道了。 在HTC手机上,reboot命令即使与SU root访问也不起作用。
需要调用BUSYBOX来执行重新启动命令。此代码适用于三星Galaxy S2、S4、索尼Xperia S(LTi 26)
经过长期的斗争,我找到了有效的解决办法 如果系统使用串行端口,则执行以下命令,
Runtime.getRuntime().exec(new String[]{"/system/bin/su","-c","reboot now"});
如果使用正常端口,则执行以下命令
Runtime.getRuntime().exec(new String[]{"/system/xbin/su","-c","reboot now"});
区别只是/bin
和/xbin
所以,可以编写这样的代码:如果第一个命令抛出异常,然后执行第二个
感谢完整的代码,我没有很好地使用进程。无法运行程序“su”:错误=13,权限denied@GauravMandlik你在这上面找到什么了吗?几个月前我已经回答了这个问题:,在这个问题之后,我做了一个开源的小应用程序:谢谢,我喜欢这个小巧的!出于某种原因,我不得不使用:Runtime.getRuntime().exec(新字符串[]{“su”、“-c”、“立即重新启动”});相反,但它起了作用。非常感谢你!它在我的三星设备中不工作,我需要给予任何许可吗?@DavidShellabarger:这是因为
su
可能在/system/xbin/
下,而不是/system/bin/
下。位置取决于你是如何获得su的,或者你是如何为你的设备设置根目录的。有问题的设备需要设置根目录。如果有人在HTC手机上遇到同样的问题,有一种更简单的方法可以绕过“不允许!”消息,而不必安装Busybox:不用调用“reboot”命令,只需写一个“b”通过调用“echo b>/proc/sysrq trigger”Runtime.getRuntime().exec(新字符串[]{“su”、“-c”、“立即重新启动”}),将其转换为/proc/sysrq trigger;似乎对这两种情况都有效不能运行程序“su”:错误=13,权限被拒绝不能运行程序“su”:错误=13,权限被拒绝
Runtime.getRuntime().exec(new String[]{"/system/xbin/su","-c","reboot now"});