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);
|
}
|
|
}
|