2
votes

I been trying to read cpu usage in my application using the @szcoder answer to How to get Memory usage and CPU usage in android? here at SOF.

His solution used to work on my Samsung Galaxy S2 (dual-core cpu), but its not working on my HTC One M7 which is quad core.

Most of the readings (Value from @szcoder's method*100 i.e percentage cpu usage) are either negative, some less than 10% and few are more than 100%.

In a comment to this answer Dave suggests that we should use multiple readings for accurate value.

Can some one please suggest a possible solution to this problem?

EDIT :

I also tried to use Fabian Knapp's solution which uses top -n 1 command to determine the CPU usage but in my testing parsing /proc/stat is much faster than top -n 1 (0.3xx seconds comapred to 3.xx seconds) so in case some one suggests it, I won't be using the top command because of time consumption.

1

1 Answers

1
votes

If the top command gives you the correct value, then I suggest you look at Android's source code for top and pull out the bit you need for CPU, which takes less time than a second. I can say this works because I did it once.