Is there any way to run terminal commands on my application and then access the data on my UI? Specifically top
.
views:
291answers:
4Check out Log Collector as an example. Here is the relevant file: http://code.google.com/p/android-log-collector/source/browse/trunk/android-log-collector/src/com/xtralogic/android/logcollector/SendLogActivity.java
The key is here:
Process process = Runtime.getRuntime().exec(commandLine);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
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.
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
Hi
I am also facing a similar issue.I am trying to execute the getevent from my application.I can see the process getevent running when I do ps on shell.But I dont see any logs for the touch events performed. Please help
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;
process = Runtime.getRuntime().exec("top -n 1 -d 1");
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}