ProAnswers.org

How to get CPU usage in android?

for CPU usage you can try

> adb shell top -m 10

Use this function to calculate cpu usage. Hope it can help you.

private float readUsage() {
try {
RandomAccessFile reader = new RandomAccessFile("/proc/stat", “r”);
String load = reader.readLine();

    String[] toks = load.split(" ");

    long idle1 = Long.parseLong(toks[5]);
    long cpu1 = Long.parseLong(toks[2]) + Long.parseLong(toks[3]) + Long.parseLong(toks[4])
          + Long.parseLong(toks[6]) + Long.parseLong(toks[7]) + Long.parseLong(toks[8]);

    try {
        Thread.sleep(360);
    } catch (Exception e) {}

    reader.seek(0);
    load = reader.readLine();
    reader.close();

    toks = load.split(" ");

    long idle2 = Long.parseLong(toks[5]);
    long cpu2 = Long.parseLong(toks[2]) + Long.parseLong(toks[3]) + Long.parseLong(toks[4])
        + Long.parseLong(toks[6]) + Long.parseLong(toks[7]) + Long.parseLong(toks[8]);

    return (float)(cpu2 - cpu1) / ((cpu2 + idle2) - (cpu1 + idle1));

} catch (IOException ex) {
    ex.printStackTrace();
}

return 0;

}