如何获得Android中的平均CPU使用率? [英] How get average CPU usage in Android?

查看:75
本文介绍了如何获得Android中的平均CPU使用率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取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.

我在此问题和<由提供的href ="https://github.com/souch/AndroidCPU/blob/master/app/src/main/java/souch/androidcpu/CpuInfo.java" rel ="nofollow noreferrer">代码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使用率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆