Linux 任何以编程方式在android上运行shell命令的方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3350283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Any way to run shell commands on android programmatically?
提问by Shouvik
Is there any way to run terminal commands on my application and then access the data on my UI? Specifically top
.
有没有办法在我的应用程序上运行终端命令,然后访问我的 UI 上的数据?具体来说top
。
采纳答案by EboMike
Check out Log Collector as an example. Here is the relevant file.
以日志收集器为例。这是相关文件。
The key is here:
关键在这里:
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()));
回答by androidworkz
it also depends on what you are running in the terminal... if you are running "cat" on a file you can also do it like this.
它还取决于您在终端中运行的内容……如果您在文件上运行“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;
}
EDIT: There is a perfect example for you in the android labs project called netmeter. There is a class called Top.java that actually does exactly what you want and it is used in TaskList.java to be displayed. http://code.google.com/p/android-labs/source/browse/#svn/trunk/NetMeter/src/com/google/android/netmeter
编辑:在名为 netmeter 的 android 实验室项目中有一个完美的例子。有一个名为 Top.java 的类实际上完全符合您的要求,它在 TaskList.java 中用于显示。 http://code.google.com/p/android-labs/source/browse/#svn/trunk/NetMeter/src/com/google/android/netmeter
回答by Shouvik
Okay this is what exactly worked for me just in case anyone needs it in the future... :)
好的,这对我有用,以防将来有人需要它...... :)
Surround in try and catch
围绕尝试和捕捉
try {
Process process = Runtime.getRuntime().exec("top -n 1 -d 1");
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
} catch (InterruptedException e) {
e.printStackTrace();
}
回答by gvk51
We, can execute commands as follow, i was succesfull in doing this....! try like this, here we need to specify the complete path of command. to get the complete path of commmand, at ur terminal (android) type
我们,可以执行如下命令,我成功地做到了......!试试这样,这里我们需要指定命令的完整路径。获取命令的完整路径,在你的终端(android)输入
*$ which ls
*$ which ls
/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);
}