【发布时间】:2011-03-21 23:40:51
【问题描述】:
有什么方法可以在我的应用程序上运行终端命令,然后访问我的 UI 上的数据?特别是top。
【问题讨论】:
有什么方法可以在我的应用程序上运行终端命令,然后访问我的 UI 上的数据?特别是top。
【问题讨论】:
以日志收集器为例。这是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()));
【讨论】:
好吧,这正是对我有用的东西,以防万一将来有人需要它... :)
包围在尝试和捕捉中
try {
Process process = Runtime.getRuntime().exec("top -n 1 -d 1");
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
} catch (InterruptedException e) {
e.printStackTrace();
}
【讨论】:
我们,可以执行如下命令,我成功了……!像这样尝试,这里我们需要指定命令的完整路径。获取命令的完整路径,在你的终端(android)输入
*$ 哪一个
/system/bin*
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);
}
【讨论】:
这还取决于您在终端中运行的内容...如果您在文件上运行“cat”,您也可以这样做。
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
【讨论】:
line.indexOf 应该是 > -1 而不是 > 0。 -1 表示未找到
对于Kotlin爱好者,可以使用以下
fun executeShell() {
val command: String = "top -n 1"
try {
val process: Process = Runtime.getRuntime().exec(command)
// Read the lines using BufferedReader
BufferedReader(InputStreamReader(process.inputStream)).forEachLine {
// Do something on each line read
Log.d(this::class.java.canonicalName, "$it")
}
} catch (e: InterruptedException) {
Log.w(this::class.java.canonicalName, "Cannot execute command [$command].", e)
} catch (e: Exception) {
Log.e(this::class.java.canonicalName, "Cannot execute command [$command].", e)
}
}
您甚至不必处理关闭缓冲区,因为 forEachLine 扩展函数会处理它。
【讨论】: