-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathAppStateController.java
More file actions
58 lines (37 loc) · 2.04 KB
/
AppStateController.java
File metadata and controls
58 lines (37 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.example.androidpowercomsumption.controller;
import android.util.Log;
import com.example.androidpowercomsumption.utils.monitor.LogFileWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
public class AppStateController {
private static final String TAG = "AppStateController";
public long startTime; // 监控开始时间
public long endTime; // 监控结束时间
public long foregroundTime; // 前台运行时长
public long backgroundTime; // 后台运行时长
public boolean status; // true 前台 false 后台
public long curStatusStartTime; // 当前状态的开始时间
public long curStatusEndTime; // 当前状态的结束时间
public double foregroundRatio; //前台运行时间占比
public double backgroundRatio; // 后台运行时间占比
public void start() {
this.startTime = System.currentTimeMillis();
}
public void finish() {
this.endTime = System.currentTimeMillis();
this.foregroundRatio = foregroundTime * 1.0 / (this.foregroundTime + this.backgroundTime);
this.backgroundRatio = backgroundTime * 1.0 / (this.foregroundTime + this.backgroundTime);
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd/HH:mm:ss");
Date startDate = new Date(this.startTime);
Date endDate = new Date(this.endTime);
Log.d(TAG, "前台运行时间:" + this.foregroundTime);
LogFileWriter.write("前台运行时间:" + this.foregroundTime + " ms");
Log.d(TAG, "后台运行时间:" + this.backgroundTime);
LogFileWriter.write("后台运行时间:" + this.backgroundTime + " ms");
Log.d(TAG, "总运行时间:" + format.format(startDate) + "~" + format.format(endDate));
Log.d(TAG, "前台运行时间占比:" + this.foregroundRatio);
LogFileWriter.write("前台运行时间占比:" + this.foregroundRatio);
Log.d(TAG, "后台运行时间占比:" + this.backgroundRatio);
LogFileWriter.write("后台运行时间占比:" + this.backgroundRatio);
}
}