get App Cpu Time - Android Hardware

Android examples for Hardware:CPU Information

Description

get App Cpu Time

Demo Code


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

import java.util.List;
import android.app.ActivityManager.RunningAppProcessInfo;

public class Main {
    public static List<RunningAppProcessInfo> mApp;

    public static long getAppCpuTime() { 
        String[] cpuInfos = null;
        long multyProcCpuTime = 0;
        for (RunningAppProcessInfo app : mApp) {
            try {
                int pid = app.pid;
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(new FileInputStream("/proc/"
                                + pid + "/stat")), 1000);
                String load = reader.readLine();
                reader.close();/*from  ww w  .j  a v  a 2  s. c om*/
                cpuInfos = load.split(" ");
                long appCpuTime = Long.parseLong(cpuInfos[13])
                        + Long.parseLong(cpuInfos[14])
                        + Long.parseLong(cpuInfos[15])
                        + Long.parseLong(cpuInfos[16]);
                multyProcCpuTime += appCpuTime;
            } catch (Exception ex) {
                ex.printStackTrace();
                multyProcCpuTime += 0;
            }
        }
        return multyProcCpuTime;
    }
}

Related Tutorials