【问题标题】:How to get CPU usage statistics on Android?如何在 Android 上获取 CPU 使用率统计信息?
【发布时间】:2011-01-28 21:11:01
【问题描述】:

我想获取 Android 上的整体 CPU 使用率,类似于 Windows 的任务管理器所做的。我可以解析Android中包含的top程序的输出,但是如果有一个API调用可以做同样的事情,那就更好了。

任何指针?

【问题讨论】:

    标签: android cpu-usage processor performance-monitor


    【解决方案1】:

    您可以参考“DevTools”项目。

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

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

    ActivityManager

    【讨论】:

    【解决方案2】:

    注意:这个答案是旧的,并且由于增强的安全机制,适用于较新版本的 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;
    }
    

    玩得开心:)

    【讨论】:

    • 不工作。我收到这个错误。引起:java.lang.NumberFormatException:对于输入字符串:“[?25l[0m[H[J[s[999C[999B[6n[uTasks:”] 在这一行:cpuUsageAsInt[i] = Integer.parseInt(myString[ i]);
    • 如何自己解析 top 并检查如何修改这些超过 6 年的 sn-p 以使其再次变得更好? ;-)
    • 现在 top 只返回我的应用程序的使用情况。我认为它不像 6 年前那样有效。 :(
    • 这是有道理的(我想知道为什么用户应用程序能够看到完整的系统 cpu 统计信息)。你在哪个安卓版本上试过?
    • 我在 Android 8.0 和 7.0 上试过。两者都没有工作。那么有没有办法获取这些信息呢?我的意思是如果我只需要获取 CPU 使用率。
    【解决方案3】:

    您可以阅读/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 % = ( idle * 100 ) / ( user + nice + system + idle + iowait + irq + softirq )
    您可以计算时间增量之间的空闲差异,并计算 CPU 使用率。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-10
      • 2011-09-05
      • 2022-10-06
      • 1970-01-01
      • 2012-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多