问题描述
我想获取Android每100毫秒或500毫秒的平均CPU使用率.我尝试使用 top
和 dumpsys cpuinfo
,但是它们都没有告诉我平均使用率或时间戳.那么如何获得平均使用量呢?
我注意到/proc/stat
中有一些CPU使用率信息,我可以使用/proc/stat
计算平均CPU使用率吗?
I want to get the average CPU usage in Android per 100ms or 500ms. I have tried to use top
and dumpsys cpuinfo
, but neither of them tell me the average usage or the timestamp. So how can I get the average usage?
I notice that there is some CPU usage information in /proc/stat
, can I calculate average CPU usage using /proc/stat
?
推荐答案
我测试了Krypton的解决方案,并得出了错误的数字(大约22%,而通过adb shell top获得了大约48%).因此,答案可能无法在android 7上正常运行,或者存在一些错误.
I've tested the solution by Krypton, and got wrong numbers (about 22% while getting about 48 via adb shell top).So maybe the answer is not running ok on android 7, or there is some bug.
我在此问题和代码Souch已经为我们做好了工作.只需使用他的CpuInfo类即可在每次需要时获取平均负载.
I found a working solution at this question ,and code provided by Souch is already doing the job for us. Just use his CpuInfo class to get the average load each time you need it.
这篇关于如何获得Android中的平均CPU使用率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!