public classAppStatusTrackerimplementsApplication.ActivityLifecycleCallbacks {
private static final longMAX_INTERVAL=5*60*1000;//保活时间
private static finalStringTAG="AppStatusTracker";
private staticAppStatusTrackertracker;
privateApplicationapplication;
private intmAppStatus= ConstantValue.STATUS_OFFLINE;
private booleanisForground;
private booleanisEffectiveForground;//针对控制器UDP服务器要求
private intactiveCount;
private longtimestamp;
private booleanisScreenOff;
privateDaemonReceiverreceiver;
privateListoList;//用于存放所有启动的Activity的集合
privateActivitylatestActivity;//最近展示的activity,不保证为null
privateAppStatusTracker(Application application) {
this.application= application;
application.registerActivityLifecycleCallbacks(this);
oList=newArrayList<>();
}
public static voidinit(Application application) {
tracker=newAppStatusTracker(application);
//初始化
tracker.setAppStatus(ConstantValue.STATUS_ONLINE);
}
public staticAppStatusTrackergetInstance() {
returntracker;
}
public voidsetAppStatus(intstatus) {
this.mAppStatus= status;
LogUtil.d(TAG,"mAppStatus"+mAppStatus);
if(status == ConstantValue.STATUS_ONLINE) {
if(receiver==null) {
IntentFilter filter =newIntentFilter();
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_SCREEN_ON);
receiver=newDaemonReceiver();
application.registerReceiver(receiver,filter);
}
}else if(status == ConstantValue.STATUS_OFFLINE) {
if(receiver!=null) {
application.unregisterReceiver(receiver);
receiver=null;
}
}
}
public intgetAppStatus() {
return this.mAppStatus;
}
public booleanisForground() {
returnisForground;
}
public booleanisEffectiveForground() {
returnisEffectiveForground;
}
private voidonScreenOff(booleanisScreenOff) {
this.isScreenOff= isScreenOff;
}
public booleancheckIfShowGesture() {
if(mAppStatus== ConstantValue.STATUS_ONLINE) {
if(isScreenOff) {
return true;
}
if(timestamp!=0l&& System.currentTimeMillis() -timestamp>MAX_INTERVAL) {
return true;
}
}
return false;
}
@Override
public voidonActivitySaveInstanceState(Activity activity,Bundle outState) {
}
@Override
public voidonActivityCreated(Activity activity,Bundle savedInstanceState) {
LogUtil.d(TAG,activity.toString() +" onActivityCreated");
if(!oList.contains(activity)) {
oList.add(activity);
}
}
@Override
public voidonActivityStarted(Activity activity) {
LogUtil.d(TAG,activity.toString() +" onActivityStarted");
activeCount++;
}
@Override
public voidonActivityResumed(Activity activity) {
if(!TextUtils.isEmpty(AppCache.getToken()) && !isEffectiveForground) {
// check it is first effective forground
isEffectiveForground=true;
LogUtil.e(TAG,activity.toString() +" onActivityResumed is first effective forground !!!");
}
LogUtil.d(TAG,activity.toString() +" onActivityResumed");
latestActivity= activity;
isForground=true;
timestamp=0L;
isScreenOff=false;
}
@Override
public voidonActivityPaused(Activity activity) {
LogUtil.d(TAG,activity.toString() +" onActivityPaused");
}
@Override
public voidonActivityStopped(Activity activity) {
LogUtil.d(TAG,activity.toString() +" onActivityStopped");
activeCount--;
if(activeCount==0) {
isForground=false;
timestamp= System.currentTimeMillis();
isEffectiveForground=false;//重新计值
LogUtil.e(TAG,activity.toString() +" onActivityStopped isn't Forground , token:"
+ AppCache.getToken());
}
}
@Override
public voidonActivityDestroyed(Activity activity) {
LogUtil.d(TAG,activity.toString() +" onActivityDestroyed");
if(latestActivity!=null&&latestActivity== activity) {
latestActivity=null;
}
if(oList.contains(activity)) {
oList.remove(activity);
}
}
/**
*获取栈顶Activity.
*
*@return栈顶Activity
*/
publicActivitygetLatestActivity() {
returnlatestActivity;
}
/**
*关闭除了指定activity以外的全部activity.
*
*@paramclsclass
*/
public voidfinishOthersActivity(Class cls) {
for(Activity activity :oList) {
if(activity.getClass().equals(cls)) {
continue;
}
if(!activity.isFinishing()) {
activity.finish();
}
}
}
/**
*销毁所有的Activity.
*/
public voidfinishAllActivity() {
//部分机型不支持stream()/java8
//oList.stream().
// filter(activity -> activity != null && !activity.isFinishing()).
// forEach(Activity::finish);
//AppStatusTracker.getInstance().setAppStatus(ConstantValue.STATUS_OFFLINE);
for(Activity activity :oList) {
if(activity !=null&& !activity.isFinishing()) {
activity.finish();
}
}
}
/**
*应用程序退出.
*/
public voidappExit(Context context) {
try{
finishAllActivity();
ActivityManager activityMgr = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
activityMgr.killBackgroundProcesses(context.getPackageName());
System.exit(0);
}catch(Exception e) {
System.exit(0);
}
}
/**
* DaemonReceiver.
*/
private classDaemonReceiverextendsBroadcastReceiver {
@Override
public voidonReceive(Context context,Intent intent) {
String action = intent.getAction();
LogUtil.d(TAG,"onReceive:"+ action);
if(Intent.ACTION_SCREEN_OFF.equals(action)) {
AppStatusTracker.getInstance().onScreenOff(true);
}else if(Intent.ACTION_SCREEN_ON.equals(action)) {
AppStatusTracker.getInstance().onScreenOff(false);
}
}
}
}