4
A
回答
5
首先确保您需要的shell命令在Android中实际可用。我遇到了一些问题,假设你可以使用>做类似重定向输出的事情。
此方法也适用于我认为是v2.2的非根用户电话,但您应该检查API参考以确保。
try {
Process chmod = Runtime.getRuntime().exec("/system/bin/chmod 777 " +fileName);
BufferedReader reader = new BufferedReader(
new InputStreamReader(nfiq.getInputStream()));
int read;
char[] buffer = new char[4096];
StringBuffer output = new StringBuffer();
while ((read = reader.read(buffer)) > 0) {
output.append(buffer, 0, read);
}
reader.close();
chmod.waitFor();
outputString = output.toString();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
虽然它可能没有必要100%,这是一个好主意,有过程等待EXEC与process.waitFor(),因为你说你关心的输出完成。
2
您需要首先确保安装了busybox,因为它会安装最常用的shell命令列表,然后使用以下代码来运行该命令。
Runtime.getRuntime().exec("ls");
相关问题
- 1. 从UEFI应用程序内运行UEFI shell命令
- 2. 如何从Mac/MacRuby应用程序运行shell命令?
- 3. 运行shell命令从Android应用程序复制文件
- 4. 执行应用程序后运行shell脚本命令
- 5. 从gulp运行shell命令
- 6. 从php运行shell命令
- 7. 从Django运行shell命令
- 8. 在c程序中运行shell命令
- 9. Ruby运行shell命令不工作(对于facebook应用程序)
- 10. 可以运行shell命令的OS X应用程序
- 11. 运行Iphone模拟器从命令行运行应用程序
- 12. 运行shell命令
- 13. 运行shell命令
- 14. 从C程序执行shell命令
- 15. 从JAVA程序运行Cygwin shell(cygwin.exe)并编写命令行
- 16. 用PHP运行shell命令?
- 17. 如何从应用程序执行shell命令
- 18. 从Foxx应用程序执行shell命令
- 19. 从.NET应用程序执行一个shell命令
- 20. 从Django应用程序/项目执行shell命令
- 21. 如何从Web应用程序运行命令行进程?
- 22. 运行命令行命令的Windows应用程序
- 23. Shell:使用Shell运行vim命令
- 24. 从shell命令访问正在运行的java程序
- 25. C#应用程序 - 从命令行运行FFMpeg不起作用
- 26. 使用shell命令更改正在运行的程序行为
- 27. 运行程序多次使用一个行shell命令
- 28. 从命令行运行Java程序
- 29. 从命令行运行Java程序
- 30. 从命令行运行Java程序
一些命令像这样(顶部等)工作,但其他返回一个空字符串,即使它从我的外壳程序运行命令时输出到终端。例如。 process = Runtime.getRuntime()。exec(“/ system/bin/ping”);任何想法为什么输入流中没有任何内容? –
啊,两个不同的问题。我使用readLine()来获取'top'的输出,但'top'输出中的第一件事是一个换行符,这样就解释了空字符串。另一件事是'ping'产生错误,并且它们在getErrorStream()而不是getInputStream()中。一切都很好:) –