get Total Cpu Time - Android Hardware

Android examples for Hardware:CPU Information

Description

get Total Cpu Time

Demo Code


//package com.java2s;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;

public class Main {
    private static long totalCpu;
    private static long totalCpuBusy;

    public static void getTotalCpuTime() { 
        String[] cpuInfos = null;
        try {/*  w  w  w  .  j  a v  a2  s  . c o m*/
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(new FileInputStream("/proc/stat")),
                    1000);
            String load = reader.readLine();
            reader.close();
            cpuInfos = load.split(" ");
            totalCpu = Long.parseLong(cpuInfos[2])
                    + Long.parseLong(cpuInfos[3])
                    + Long.parseLong(cpuInfos[4])
                    + Long.parseLong(cpuInfos[6])
                    + Long.parseLong(cpuInfos[5])
                    + Long.parseLong(cpuInfos[7])
                    + Long.parseLong(cpuInfos[8]);
            totalCpuBusy = Long.parseLong(cpuInfos[2])
                    + Long.parseLong(cpuInfos[3])
                    + Long.parseLong(cpuInfos[4])
                    + Long.parseLong(cpuInfos[6])
                    + Long.parseLong(cpuInfos[7])
                    + Long.parseLong(cpuInfos[8]);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

Related Tutorials