Is there any way to run terminal commands on my application and then access the data on my UI? Specifically top
.
有没有办法在我的应用程序上运行终端命令,然后访问UI上的数据?特别是上面。
4 个解决方案
#1
33
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()));
#2
15
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();
}
#3
8
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
我们可以执行命令,我在做这个succesfull .... !试试这样,这里我们需要指定命令的完整路径。要获得完整的指令路径,请使用ur终端(android)类型
*$ which ls
* $ ls的
/system/bin*
/系统/ 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);
}
#4
5
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
编辑:android实验室项目netmeter就是一个很好的例子。有一个类叫做Top。java,它能做你想做的事,在任务列表中使用。java显示。http://code.google.com/p/android-labs/source/browse/ svn /箱子/ NetMeter / src /com/google/android/netmeter
#1
33
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()));
#2
15
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();
}
#3
8
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
我们可以执行命令,我在做这个succesfull .... !试试这样,这里我们需要指定命令的完整路径。要获得完整的指令路径,请使用ur终端(android)类型
*$ which ls
* $ ls的
/system/bin*
/系统/ 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);
}
#4
5
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
编辑:android实验室项目netmeter就是一个很好的例子。有一个类叫做Top。java,它能做你想做的事,在任务列表中使用。java显示。http://code.google.com/p/android-labs/source/browse/ svn /箱子/ NetMeter / src /com/google/android/netmeter