1.参考文档

https://www.jianshu.com/p/6bf564f7cdf0

2.源码下载

https://github.com/sufadi/AndroidCpuTools

3.思路

获取”/proc/stat”的运行时间,使用CPU 使用率 =(CPU 非空闲时间2 - CPU 非空闲时间1) / (CPU 运行总时间2 - CPU 运行总时间1) * 100%计算

4.实现代码

package com.fadisu.cpurun.util;

import android.os.Process;

import java.util.ArrayList;
import java.util.List;

/**
 * ProcCpuStatUtil analysis
 */
public class ProcCpuStatUtil {

    private static final String TAG = ProcCpuStatUtil.class.getSimpleName();

    // ProcCpuStatUtil probe
    private static final int[] SYSTEM_CPU_FORMAT = new int[]{
            Process.PROC_SPACE_TERM | Process.PROC_COMBINE,
            Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 1: user time
            Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 2: nice time
            Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 3: sys time
            Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 4: idle time
            Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 5: iowait time
            Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 6: irq time
            Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG  // 7: softirq time
    };

    public static List<String> getCpuTime() {
        List<String> result = new ArrayList<>();
        long[] sysCpu = new long[7];
        if (Process.readProcFile("/proc/stat", SYSTEM_CPU_FORMAT, null, sysCpu, null)) {
            result.add("User time : " + DateTimeUtil.longToSting(sysCpu[0]));
            result.add("Nice time : " + DateTimeUtil.longToSting(sysCpu[1]));
            result.add("Sys time : " + DateTimeUtil.longToSting(sysCpu[2]));
            result.add("Idle time : " + DateTimeUtil.longToSting(sysCpu[3]));
            result.add("Iowait time : " + DateTimeUtil.longToSting(sysCpu[4]));
            result.add("Irq time : " + DateTimeUtil.longToSting(sysCpu[5]));
            result.add("Softirq time : " + DateTimeUtil.longToSting(sysCpu[6]));

            long totalCpuTime = sysCpu[0] + sysCpu[1] + sysCpu[2] + sysCpu[3] + sysCpu[4] + sysCpu[5] + sysCpu[6];
            result.add("Total CPU time : " + DateTimeUtil.longToSting(totalCpuTime));
        }

        return result;
    }

    public static class CpuRateInfo {
        // CPU 运行总时间
        double totalCpuTime;

        // CPU 空闲时间
        double idleTime;

        // CPU 非空闲时间
        double nonIdleTime;
    }

    public static CpuRateInfo getCpuRateInfo() {
        CpuRateInfo mCpuRateInfo = new CpuRateInfo();
        long[] sysCpu = new long[7];
        if (Process.readProcFile("/proc/stat", SYSTEM_CPU_FORMAT, null, sysCpu, null)) {
            mCpuRateInfo.idleTime = sysCpu[3];
            mCpuRateInfo.totalCpuTime = sysCpu[0] + sysCpu[1] + sysCpu[2] + sysCpu[3] + sysCpu[4] + sysCpu[5] + sysCpu[6];
            mCpuRateInfo.nonIdleTime = mCpuRateInfo.totalCpuTime - mCpuRateInfo.idleTime;
        }

        return mCpuRateInfo;
    }

    /**
     * 获取 CPU 使用率
     * CPU 使用率 =(CPU 非空闲时间2 - CPU 非空闲时间1) / (CPU 运行总时间2 - CPU 运行总时间1) * 100%
     * https://www.jianshu.com/p/6bf564f7cdf0
     */
    public static String getCpuUsage() {
        String result = null;
        CpuRateInfo mCpuRateInfo1 = getCpuRateInfo();

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        CpuRateInfo mCpuRateInfo2 = getCpuRateInfo();

        double usage = ((mCpuRateInfo2.nonIdleTime - mCpuRateInfo1.nonIdleTime) / (mCpuRateInfo2.totalCpuTime - mCpuRateInfo1.totalCpuTime)) * 100;
        result = String.valueOf((int) usage) + "%";
        return result;
    }

}

5.实现代码

运行结果
CPU使用率

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐