help with widget

Post your questions and help other users.

Moderator: Martin

Post Reply
doc
Posts: 5
Joined: 02 Sep 2015 19:16

help with widget

Post by doc » 02 Nov 2015 15:01

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

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: help with widget

Post by Martin » 02 Nov 2015 19:56

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

doc
Posts: 5
Joined: 02 Sep 2015 19:16

Re: help with widget

Post by doc » 02 Nov 2015 22:48

thanks, that seems to work.

Post Reply