best to stick out the code and introduce away .
------ Solution ---------------------- ----------------------
http://download.csdn.net/detail/zhoujiyu123/4441637
There is a ready-made code
------ eference ------------------------------- --------
Thank you for your reply.
you send this link is to obtain the total system CPU usage.
I need to get the CPU usage of the application .
------ eference ------------------------ ---------------
has been resolved . acquisition system CPU time and application CPU usage time for comparison drawn application CPU usage
paste the following code :
private static long getTotalCpuTime() { // 获取系统总CPU使用时间
String[] cpuInfos = null;
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
new FileInputStream("/proc/stat")), 1000);
String load = reader.readLine();
reader.close();
cpuInfos = load.split(" ");
} catch (IOException ex) {
ex.printStackTrace();
}
long 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]);
return totalCpu;
}
private static long getAppCpuTime() { // 获取应用占用的CPU时间
String[] cpuInfos = null;
try {
int pid = android.os.Process.myPid();
BufferedReader reader = new BufferedReader(new InputStreamReader(
new FileInputStream("/proc/" + pid + "/stat")), 1000);
String load = reader.readLine();
reader.close();
cpuInfos = load.split(" ");
} catch (IOException ex) {
ex.printStackTrace();
}
long appCpuTime = Long.parseLong(cpuInfos[13])
+ Long.parseLong(cpuInfos[14]) + Long.parseLong(cpuInfos[15])
+ Long.parseLong(cpuInfos[16]);
return appCpuTime;
}
------ eference ------------------------------------- -
course, you can use the ADB shell command line to see the CPU usage :
1. usb phone connection
2. at the command line adb shell, into the shell .
3 Type the command top-m 100 | grep com *** ***, followed by the name of the application package grep is used to filter the key . word
as shown below:
C: \ Users \ David Ding> adb shell
root @ p940 :/ # top-m 100 | grep com.ding.dingrecorder
top-m 100 | grep com.ding.dingrecorder
24807 1 0% S 11 501096K 45520K fg u0_a68 com.ding.dingrecorder
24807 1 5% S 11 501232K 45676K fg u0_a68 com.ding.dingrecorder < br>
没有评论:
发表评论