有没有办法在我的应用程序上运行终端命令,然后访问我的用户界面上的数据?具体为top
。以编程方式在android上运行shell命令的任何方式?
回答
查看日志收集器作为示例。这里是relevant file。
的关键是在这里:
ArrayList<String> commandLine = new ArrayList<String>();
commandLine.add("logcat");//$NON-NLS-1$
[...]
Process process = Runtime.getRuntime().exec(commandLine);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
这也取决于你在终端运行的是什么...如果你是在你也可以做这样的一个文件上运行“猫”。
final private String MEM_FILE = "/proc/meminfo";
public Long readMem() {
String[] segs;
FileReader fstream;
try {
fstream = new FileReader(MEM_FILE);
} catch (FileNotFoundException e) {
Log.e("readMem", "Could not read " + MEM_FILE);
return false;
}
BufferedReader in = new BufferedReader(fstream, 500);
String line;
try {
while ((line = in.readLine()) != null) {
if (line.indexOf("MemTotal:") > 0) {
Log.e("MemTotal", line);
segs = line.trim().split("[ ]+");
memTotal = Long.parseLong(segs[1]);
}
if (line.indexOf("MemFree:") > 0) {
Log.e("MemFree", line);
segs = line.trim().split("[ ]+");
memFree = Long.parseLong(segs[1]);
}
}
updateMem(); //call function to update textviews or whatever
return true;
} catch (IOException e) {
Log.e("readMem", e.toString());
}
return false;
}
编辑: 有一个名为netmeter的Android实验项目给你一个很好的例子。有一个名为Top.java的类实际上完全按照您的需要进行,并将其用于TaskList.java中以供显示。 http://code.google.com/p/android-labs/source/browse/#svn/trunk/NetMeter/src/com/google/android/netmeter
不是猫,我特别想跑到顶端,并获得屏幕上显示的实时结果。我知道模拟终端的应用程序,我发现它的顶级工作,所以我想知道如何才能拉出顶部的结果并在UI上显示它们。 – Shouvik 2010-07-28 11:33:50
查看我上面的编辑,了解您想要的正确工作示例。 – androidworkz 2010-07-28 12:42:31
嘿谢谢...需要在这里度过一段时间,但我认为这是...你一直在帮助很大! – Shouvik 2010-07-29 05:05:58
好吧,这到底是什么,以防万一在尝试为我工作的人需要它在未来... :)
环绕,赶上
try {
Process process = Runtime.getRuntime().exec("top -n 1 -d 1");
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
} catch (InterruptedException e) {
e.printStackTrace();
}
当我这样做后跟String result = null; result = bufferedReader.readLine(); Log.i(“OnClickListener”,“result:”+ result);它只显示“result:”('result'字符串为空)。任何想法,为什么会这样?另外,当我尝试process = Runtime.getRuntime()。exec(“/ system/bin/ping abc”);相反,'结果'字符串为空。这两个命令都可以像'adb shell'终端一样按预期工作。 – 2013-09-17 17:15:40
对不起@DavidDoria,我现在两年没碰过android。这是我在大学期间尝试过的一些事情。你最好把它作为一个问题发布。 – Shouvik 2013-09-20 16:01:13
我得到它的工作。问题是,'top'的第一行是空的(一个新行,创建表)。实际输出时它返回null的情况是因为我需要得到stderror(getErrorStream)而不是stdout(getInputStream)。 – 2013-09-20 17:38:17
我们,可以执行命令,跟着,我成功地做到了这一点....!像这样尝试,这里我们需要指定完整的命令路径。获得条命令的完整路径,在乌尔终端(机器人)型
* $其中LS
/系统/箱*
try {
// Executes the command.
Process process = Runtime.getRuntime().exec("/system/bin/ls /sdcard");
// Reads stdout.
// NOTE: You can write to stdin of the command using
// process.getOutputStream().
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.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();
// Waits for the command to finish.
process.waitFor();
return output.toString();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
- 1. Android |以编程方式运行命令
- 2. 以编程方式执行shell命令
- 3. 如何以编程方式等待Shell命令完成运行?
- 4. 以编程方式执行vim命令?
- 5. PseudoTTY以编程方式执行命令
- 6. 通过命令行以编程方式运行程序
- 7. 如何以编程方式调用Felix/Karaf shell命令?
- 8. 如何打开网页并以编程方式在其上运行JavaScript命令?
- 9. 以编程方式打开Gnome终端并运行命令
- 10. 以编程方式创建/运行命令文件
- 11. 以编程方式在android中的FFmpeg命令
- 12. 以编程方式在android
- 13. 以任何方式在.NET中以编程方式添加HttpHandler?
- 14. 以编程方式在Android上动画
- 15. 重命名Android apk以编程方式
- 16. 我需要什么权限才能在Android中以编程方式运行shell命令?
- 17. 以便携方式以编程方式更改shell的cwd
- 18. 是否有任何非Windows方式从本地.html文件运行shell命令?
- 19. 如何从node.js以编程方式执行mongodump命令?
- 20. 在Eclipse控制台中以编程方式执行命令
- 21. 如何以编程方式在android中执行所有可能的unix(shell)命令?
- 22. 以编程方式确定Python的最大命令行长度
- 23. 以编程方式运行Cakefile
- 24. 以编程方式运行多个类
- 25. 如何以异步方式在Perl中运行系统命令?
- 26. 以编程方式获取在android中运行的任务快照
- 27. 任何方式来解释一个命令而不运行cmd.exe?
- 28. 以编程方式更改Windows Shell
- 29. 的Android编程:如何以编程方式在网格的方式
- 30. 从命令行或以编程方式更改配置参数
所以基本上,如果我想上运行,我可以只需用顶部替换commandLine,它应该正常工作吗?我得到一个错误,所以我想我需要更多的帮助..谢谢.. – Shouvik 2010-07-28 11:40:57
我是否需要包围进程process = Runtime.getRuntime()。exec(commandLine);尝试和赶上,因为我不断收到这抛出IOException。我看了java的例子似乎处理它的方式... – Shouvik 2010-07-28 12:33:06
是的..再次,看看链接,看看他们做的一切。你肯定需要做错误检查 - 不要指望'顶部'出现在所有手机上。 – EboMike 2010-07-28 16:15:07