Reputation: 849
I'hv been struggling to get the accurate CPU Usage per process and process Idle CPU Time.. I tried " top " command proc/[pid]/stat folder but still trying my luck..
TOP command gives CPU usage for all the processes running in fg(foreground) and bg(background) but it is not as accurate i feel.. because it shows %CPU 0, even if the process is running in the background.. Any other way? please help
Upvotes: 3
Views: 7867
Reputation: 1722
You can get CPU usage with:
ArrayList<String> list = new ArrayList<String>();
try {
// -m 10, how many entries you want, -d 1, delay by how much, -n 1,
// number of iterations
Process p = Runtime.getRuntime().exec("top -m 15 -d 1 -n 1");
BufferedReader reader = new BufferedReader(new InputStreamReader(
p.getInputStream()));
int i = 0;
String line = reader.readLine();
while (line != null) {
Log.e("Output " i, line);
list.add(line);
line = reader.readLine();
i ;
}
p.waitFor();
Toast.makeText(getBaseContext(), "Got update",Toast.LENGTH_SHORT)
.show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getBaseContext(), "Caught", Toast.LENGTH_SHORT)
.show();
}
Upvotes: 1
Reputation: 849
I have found a way to do it myself.. I am using
Upvotes: 2