显示Shell脚本的进程
我有以下方法在我的 java 应用程序中运行 shell 命令,并且我希望运行一些脚本,例如修复用户手机上所有应用程序权限的脚本。我可以使用此命令 execCommand("/system/xbin/fix_perm"); 毫无问题地运行脚本但问题是我只想打印出正在执行的操作,就像终端模拟器一样,我如何获取输出流并将其打印在屏幕上?感谢您的帮助
public Boolean execCommand(String command)
{
try {
Runtime rt = Runtime.getRuntime();
Process process = rt.exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
os.writeBytes(command + "\n");
os.flush();
os.writeBytes("exit\n");
os.flush();
process.waitFor();
} catch (IOException e) {
return false;
} catch (InterruptedException e) {
return false;
}
return true;
}
I have the following method for running shell commands in my java applications and i'm looking to run a few scripts such as one that fixes permissions for all applications on the users phone. I can run the script no problem by using this command execCommand("/system/xbin/fix_perm"); however the problem is that i want to just print out what's being done like terminal emulator does how can i take my outputstream and print it on the screen? Thank you for any help
public Boolean execCommand(String command)
{
try {
Runtime rt = Runtime.getRuntime();
Process process = rt.exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
os.writeBytes(command + "\n");
os.flush();
os.writeBytes("exit\n");
os.flush();
process.waitFor();
} catch (IOException e) {
return false;
} catch (InterruptedException e) {
return false;
}
return true;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我希望您了解允许用户以
su
形式运行任意命令可能带来的极端后果,并指出可能的解决方案。I'm hoping you're aware of the potentially extreme consequences of allowing a user to run arbitrary commands as
su
and will instead point to a possible solution.