whyclj
2019-12-31 d2272f21a9d4491f22b3d3c9d6dbfebcecff76e3
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.mytestapp;
 
 
import android.util.Log;
 
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
 
public class CrashMonitor {
 
    private final static  String TAG = "CrashMonitor";
    private ScheduledExecutorService executorService  = null;
    private long oldpos = 0;
    private int pauseNum = 0;
    private OnCrashedListener onCrashedListener;
    private static CrashMonitor crashMonitor;
 
    private CrashMonitor(){
 
    }
 
    public static CrashMonitor createCrashMonitor(){
        if(crashMonitor == null){
            crashMonitor = new CrashMonitor();
        }
        crashMonitor.executorService = null;
        crashMonitor.oldpos = new Date().getTime();
        crashMonitor.pauseNum = 0;
        if(crashMonitor.executorService == null){
            crashMonitor.monitorPageCrashed(1500);
        }
        return crashMonitor;
    }
 
    public void destory(){
        this.executorService.shutdown();
        this.executorService = null;
        onCrashedListener = null;
    }
 
    public interface OnCrashedListener {
        void onCrashed();
    }
    public void setOnCrashedListener(OnCrashedListener listener) {
        onCrashedListener = listener;
    }
    public boolean isWorking(){
        boolean res = false;
        if(executorService != null){
            res = true;
        }
        return res;
    }
    public void updateTimer(long pos){
        oldpos = pos;
    }
    public void monitorPageCrashed(final int period){
        executorService  = Executors.newScheduledThreadPool(1);
        executorService.scheduleAtFixedRate(
                new Runnable() {
                    @Override
                    public void run() {
                        Log.e(TAG, "run: "+ (new Date().getTime() - oldpos)+"##############" + pauseNum);
                        if(oldpos > 0 && (new Date().getTime() - oldpos) > 1 * 60 * 1000){
                            pauseNum ++;
                        }
                        if(pauseNum >= 3){
                            oldpos = 0;
                            executorService.shutdown();
                            //do something
                            if(onCrashedListener != null){
                                onCrashedListener.onCrashed();
                            }
                            return;
                        }
                    }
                },0,period, TimeUnit.MILLISECONDS);
    }
 
}