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
			
									
									
						help with widget
Moderator: Martin
Re: help with widget
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
Regards,
Martin
			
									
									
						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);
}
Martin
