Any way to run shell commands on android programmatically?


Question

Is there any way to run terminal commands on my application and then access the data on my UI? Specifically top.

1
37
7/30/2010 1:56:31 AM

Accepted Answer

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()));
34
1/30/2013 9:24:02 PM

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();
}

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon