App后台切换前台可以通过ActivityLifecycleCallbacks方法来实现,ActivityLifecycleCallbacks可以用来监听app中所有Activity的声明周期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
33public void unRegister(Application application){
application.unregisterActivityLifecycleCallbacks(activityLifecycleCallbacks);
}
private Application.ActivityLifecycleCallbacks activityLifecycleCallbacks = new Application.ActivityLifecycleCallbacks() {
//打开的Activity数量统计
private int activityStartCount = 0;
@Override
public void onActivityStarted(Activity activity) {
activityStartCount++;
//数值从0变到1说明是从后台切到前台
if (activityStartCount == 1){
//从后台切到前台
if(mOnAppStatusListener != null){
mOnAppStatusListener.onFront();
}
}
}
@Override
public void onActivityStopped(Activity activity) {
activityStartCount--;
//数值从1到0说明是从前台切到后台
if (activityStartCount == 0){
//从前台切到后台
if(mOnAppStatusListener != null){
mOnAppStatusListener.onBack();
}
}
}
};