如何通过android应用程序向终端发送命令并获取输出?例如,发送“ls /”并获取输出以在GUI中打印它?如何在Android应用程序中运行终端命令?
12
A
回答
11
你必须使用反射来调用android.os.Exec.createSubprocess():
public String ls() {
Class<?> execClass = Class.forName("android.os.Exec");
Method createSubprocess = execClass.getMethod("createSubprocess", String.class, String.class, String.class, int[].class);
int[] pid = new int[1];
FileDescriptor fd = (FileDescriptor)createSubprocess.invoke(null, "/system/bin/ls", "/", null, pid);
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fd)));
String output = "";
try {
String line;
while ((line = reader.readLine()) != null) {
output += line + "\n";
}
}
catch (IOException e) {}
return output;
}
1
不同的解决方案可以在这里找到:http://code.google.com/p/market-enabler/wiki/ShellCommands 我没有测试他们没有。
1
试试这个答案有办法运行在Android shell命令编程 https://stackoverflow.com/a/3350332/2425851
相关问题
- 1. 在应用程序的终端中运行命令
- 2. 在OSX应用程序中运行终端命令?
- 3. 在Android程序中运行Linux终端命令
- 4. 如何在我的Cocoa应用程序中运行终端命令?
- 5. Android:如何在android应用程序中运行adb命令?
- 6. Linux和终端:如何在终端预约中运行命令?
- 7. 如何在java程序的终端运行命令?
- 8. 从GUI应用程序运行终端命令?
- 9. 呼叫终端和运行命令通过Java应用程序
- 10. 运行应用程序命令打开终端
- 11. 在Android应用程序运行终端命令可将音频路由
- 12. 在jsp应用程序中执行终端命令
- 13. 如何从终端运行Vim命令
- 14. 在终端中运行各种命令
- 15. 如何使用终端运行特定的Android应用程序?
- 16. 使用java程序在linux终端运行命令?
- 17. 如何以编程方式在android中执行终端命令
- 18. 如何在终端中使用vsim命令运行Modelsim?
- 19. 如何在Ubuntu的终端中自动运行应用程序?
- 20. 如何在Ubuntu终端中运行PyQt4应用程序?
- 21. 使用命令终端运行程序的问题
- 22. 无法运行java程序中的特定终端命令(Eclipse)
- 23. 如何在Android应用程序中运行重命名shell命令(已过滤)
- 24. 使用NSTask运行终端命令
- 25. 运行终端命令在CentOS
- 26. 如何以root用户身份在Android终端中运行AT命令?
- 27. 如何从Android应用程序运行adb push或pull命令?
- 28. 如何从Android应用程序运行root命令?
- 29. 如何从Android应用程序运行Tasker命令?
- 30. 如何在PHP脚本中运行Android(命令行)应用程序?
什么是execClass? – 2010-05-16 10:27:08
糟糕,那是反映出来的阶级。 – 2010-05-17 10:50:51
我得到“java.lang.ClassNotFoundException:android.os.Exec” – ademar111190 2013-09-18 20:40:58