Page 1 of 1
help with widget
Posted: 02 Nov 2015 15:01
by doc
I'm trying to create a widget which shows me the process name and its cpu usage. I'm getting the information using top(which is slow. any better ideas?) . unfortunately I can't find a simple way to select the first five lines of the output of top and then select the right columns to save the entries to a list.
in the end I want to have something like this:
[com.system.android,8%,com.someotherprocess, 2%,...]
help would be appreciated.
thanks
Re: help with widget
Posted: 02 Nov 2015 19:56
by Martin
Hi,
You could use top to get the output of the 5 top processes and then use a script to extract the values.
Something like this might work (output of top might be different depending on device):
-action
Execute Command: top -m 5 -n 1 (to get the output of 5 processes)
-action
Script
Code: Select all
// split output into lines
lines = split(stdout, '\n');
// remove lines until we have the 5 last lines left
while (length(lines) > 5)
{
removeElement(lines, 0);
}
// prepare empty list with results
result = newList();
// process each of the 5 lines
for (line in lines)
{
// split on whitespace
tokens = split(trim(line), '\\s+');
// extract cpu column
cpu = getElement(tokens, 2);
// last element contains the process name
process = getElement(tokens, length(tokens)-1);
// store processname and cpu in the result list
addElement(result, process + ' ' + cpu);
}
Regards,
Martin
Re: help with widget
Posted: 02 Nov 2015 22:48
by doc
thanks, that seems to work.