Android上如何获取CPU使用率统计信息?

发布于 2024-08-25 04:57:06 字数 140 浏览 16 评论 0原文

我想获得 Android 上的总体 CPU 使用情况,类似于 Windows 的任务管理器。我可以解析 Android 中包含的 top 程序的输出,但如果有一个 API 调用可以执行相同的操作,那就更好了。

有什么指点吗?

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.

Any pointers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小ぇ时光︴ 2024-09-01 04:57:08

注意:此答案已过时,并且由于增强的安全机制,不适用于较新版本的 Android。

对于完整的 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;
}

享受乐趣:)

ATTENTION: This answer is old and does NOT work on newer versions of Android due to enhanced security mechanisms.

For complete CPU usage (not for each process) you can use:

    /**
 * 
 * @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 :)

Saygoodbye 2024-09-01 04:57:08

您可以读取 /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

平均空闲百分比:
X % = (空闲* 100)/(用户+nice+系统+空闲+iowait+irq+softirq)

您可以计算时间增量之间的空闲差异,并计算 CPU 使用率。

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:

 - 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.

栩栩如生 2024-09-01 04:57:08

您可以参考“DevTools”项目。

使用 ActivityManager 您可以获得很多信息,例如 ActivityManager.RunningAppProcessInfo、ActivityManager.RunningTaskInfo,...

但我不确定结果是否与“top”命令相同。


ActivityManager

You can reference the "DevTools" project.

Using ActivityManager you can get lots information, such as ActivityManager.RunningAppProcessInfo, ActivityManager.RunningTaskInfo, ...

But I am not sure the result will same as 'top' command.

see
ActivityManager

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文