如何在 Android 上获取 CPU 使用率统计信息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2467579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get CPU usage statistics on Android?
提问by Randy Sugianto 'Yuku'
I want to get the overall CPU usage on Android, similar to what Windows' Task Manager does. I can parse the output of the top
program included in Android, but if there is a API call that does the same thing, it would be better.
我想获得 Android 上的整体 CPU 使用率,类似于 Windows 的任务管理器所做的。我可以解析top
Android中包含的程序的输出,但是如果有一个API调用可以做同样的事情,那就更好了。
Any pointers?
任何指针?
回答by Fabian Knapp
ATTENTION: This answer is old and does NOTwork on newer versions of Android due to enhanced security mechanisms.
注意:这个答案是旧的,也不要在Android上的由于增强的安全机制较新版本。
For complete CPU usage (not for each process) you can use:
对于完整的 CPU 使用率(不是每个进程),您可以使用:
/**
*
* @return integer Array with 4 elements: user, system, idle and other cpu
* usage in percentage.
*/
private int[] getCpuUsageStatistic() {
String tempString = executeTop();
tempString = tempString.replaceAll(",", "");
tempString = tempString.replaceAll("User", "");
tempString = tempString.replaceAll("System", "");
tempString = tempString.replaceAll("IOW", "");
tempString = tempString.replaceAll("IRQ", "");
tempString = tempString.replaceAll("%", "");
for (int i = 0; i < 10; i++) {
tempString = tempString.replaceAll(" ", " ");
}
tempString = tempString.trim();
String[] myString = tempString.split(" ");
int[] cpuUsageAsInt = new int[myString.length];
for (int i = 0; i < myString.length; i++) {
myString[i] = myString[i].trim();
cpuUsageAsInt[i] = Integer.parseInt(myString[i]);
}
return cpuUsageAsInt;
}
private String executeTop() {
java.lang.Process p = null;
BufferedReader in = null;
String returnString = null;
try {
p = Runtime.getRuntime().exec("top -n 1");
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
while (returnString == null || returnString.contentEquals("")) {
returnString = in.readLine();
}
} catch (IOException e) {
Log.e("executeTop", "error in getting first line of top");
e.printStackTrace();
} finally {
try {
in.close();
p.destroy();
} catch (IOException e) {
Log.e("executeTop",
"error in closing and destroying top process");
e.printStackTrace();
}
}
return returnString;
}
Have fun with it :)
玩得开心:)
回答by XCH
You can reference the "DevTools" project.
您可以参考“DevTools”项目。
Using ActivityManager you can get lots information, such as ActivityManager.RunningAppProcessInfo, ActivityManager.RunningTaskInfo, ...
使用 ActivityManager,您可以获得大量信息,例如 ActivityManager.RunningAppProcessInfo、ActivityManager.RunningTaskInfo、...
But I am not sure the result will same as 'top' command.
但我不确定结果是否与“top”命令相同。
see ActivityManager
见 活动管理器
回答by lidor
You can read /proc/stat
and parse the file contents. The first line is like:cpu 79242 0 74306 842486413 756859 6140 67701 0
The meanings of the columns are as follows, from left to right:
您可以读取/proc/stat
和解析文件内容。第一行是这样的:cpu 79242 0 74306 842486413 756859 6140 67701 0
各列的含义如下,从左到右:
- 1st column : user = normal processes executing in user mode
- 2nd column : nice = niced processes executing in user mode
- 3rd column : system = processes executing in kernel mode
- 4th column : idle = twiddling thumbs
- 5th column : iowait = waiting for I/O to complete
- 6th column : irq = servicing interrupts
- 7th column : softirq = servicing softirqs
Average idle percentage :X % = ( idle * 100 ) / ( user + nice + system + idle + iowait + irq + softirq )
You can compute the difference in idle between time deltas, and figure CPU usage.
平均空闲百分比:X % = ( idle * 100 ) / ( user + nice + system + idle + iowait + irq + softirq )
您可以计算时间增量之间的空闲差异,并计算 CPU 使用率。