tags:

views:

291

answers:

4

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

+1  A: 

Check 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()));
EboMike
so basically if I would like to run top, I could just replace commandLine with top and it should work fine right? I am getting an error so I guess I will need a little more help.. thanks..
Shouvik
Do I need to surround the Process process = Runtime.getRuntime().exec(commandLine); with a try and catch because I keep getting this throws IOException. I have looked at the java examples seem to handle it that way...
Shouvik
Yes.. again, check out the link to see everything they do. You definitely need to do error checking - don't expect 'top' to be there on all phones.
EboMike
+2  A: 

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

androidworkz
Not cat, I specifically want to run top and get the live results displayed on the screen. I am aware of applications which emulate the terminal on which I have found top working, so I was wondering how I could just pull the results of top and display them on the UI.
Shouvik
See my edits above for a working example of exactly what you want.
androidworkz
Hey thanks... Need to spend some time here, but I think this is it... You have been a great help!
Shouvik
A: 

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

Androider
Yep the link provided is a very decent solution to the problem. Post these types of comments as comments under comments in the tab above/
Shouvik
A: 

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