判断当前应用是在前台还是后台
12/14/220 更新 每日一问 | 如何判断应用退出,或者到后台了?
1. 应用场景
接收推送的时候,当我们的APP 收到一条推送的时候,可能在应用内,也可能在应用外。
在应用内的时候,显示一个 Dialog 之类,在应用外的时候,弹一个notifyCation.
2. 如何判断是在显示还是没有显示
2.1 方法一
以前 Android 提供 API 是 getRunningTasks(),拿到所有正在运行的 Task,一个个进行判断。 如果自己的 package 在里面,就是在应用内。
但是每一次都需要 for - loop,比较操蛋,不做介绍
2.1 方法二
Android 4.0 之后 提供 API Application.ActivityLifecycleCallbacks,生命周期执行到哪些,就会回调哪些,
public interface ActivityLifecycleCallbacks {
void onActivityCreated(Activity activity, Bundle savedInstanceState);
void onActivityStarted(Activity activity);
void onActivityResumed(Activity activity);
void onActivityPaused(Activity activity);
void onActivityStopped(Activity activity);
void onActivitySaveInstanceState(Activity activity, Bundle outState);
void onActivityDestroyed(Activity activity);
}
public class AppStatusTracker implements Application.ActivityLifecycleCallbacks{
//用来记录App状态信息
}
每次生命周期变动都会回调,收集起来就可以知道当前应用的状态
如果在应用外,所有的 Activity 都应该 onStop() 掉,或者 onDestroyed(),如果是在应用内,则只有一个 Activity 是 onResumed()。
根据Activity 之间跳转,的生命周期记录
A - -> onPaused()
B - -> onCreated()
B - -> onStarted()
B - -> onResumed()
A - -> onStoped()
A - -> onDestoryed()
//界面要显示时调用此方法,onCreate -- onResume 之间不该写大量代码,
// 防止初始化时间过长会导致黑屏,
//一旦此方法回调,就可以确定当前应用是在前台执行
@Override
public void onActivityResumed(Activity activity) {
L.e(activity.toString()+" onActivityResumed");
//Activity 跳转过程中,onPause -> onResume 之间需要时间,这时候收到消息推送,这算前台?后台?
//onPause() 可能为 false,也可能是要跳转到另外一个Activity,另外一个Activity 去掉用onResumed 方法
isForground = true;
timestamp = 0l;
isScreenOff = false;
}
@Override
public void onActivityStarted(Activity activity) {
L.e(activity.toString()+" onActivityStarted");
activeCount ++;
}
@Override
public void onActivityStopped(Activity activity) {
L.e(activity.toString()+" onActivityStopped");
activeCount --;
if (activeCount == 0){
isForground = false;//在后台执行
timestamp = System.currentTimeMillis();
}
}
3. 代码
public class AppStatusTracker implements Application.ActivityLifecycleCallbacks{
private boolean isForground;
private int activeCount;//计数器,所有的Activity都执行了stop方法,那么count=0,app在后台执行
private static final long MAX_INTERVAL = 5 * 60* 1000;
private long timestamp;//在后台呆的时间够长,再切换到前台的时间差,超过最大值显示图形解锁
private boolean isScreenOff;//图形解锁的特殊需求,屏幕锁住之后再亮起需要图形解锁
private static AppStatusTracker tracker;
private Application application;
private DaemonReceiver receiver;
private int mAppStatus = ConstantValues.STATUS_FORCE_KILLED;
private AppStatusTracker(Application application) {
this.application = application;
application.registerActivityLifecycleCallbacks(this);//必须注册才能接受到该方法的回调
}
public static void init(Application application) {
tracker = new AppStatusTracker(application);
}
public static AppStatusTracker getInstance() {
return tracker;
}
public void setAppStatus(int status) {
this.mAppStatus = status;
if (status == ConstantValues.STATUS_ONLINE){
if (receiver == null){
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_OFF);
receiver = new DaemonReceiver();
application.registerReceiver(receiver,filter);
}else if (receiver != null){
application.unregisterReceiver(receiver);
receiver = null;
}
}
}
public int getAppStatus() {
return this.mAppStatus;
}
public boolean isForground() {
return isForground;
}
private void onScreenOff(boolean isScreenOff) {
this.isScreenOff = isScreenOff;
}
public boolean checkIfShowGesture(){
if (mAppStatus == ConstantValues.STATUS_OFFLINE){
if (isScreenOff){
return false;
}
if (timestamp != 0l && System.currentTimeMillis() - timestamp > MAX_INTERVAL) {
return true;
}
}
return false;
}
@Override
public void onActivityStopped(Activity activity) {
L.e(activity.toString()+" onActivityStopped");
activeCount --;
if (activeCount == 0){
isForground = false;//在后台执行
timestamp = System.currentTimeMillis();
}
}
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
L.e(activity.toString()+" onActivityCreated");
}
@Override
public void onActivityStarted(Activity activity) {
L.e(activity.toString()+" onActivityStarted");
activeCount ++;
}
//界面要显示时调用此方法,onCreate -- onResume 之间不该写大量代码,
// 防止初始化时间过长会导致黑屏,
//一旦此方法回调,就可以确定当前应用是在前台执行
@Override
public void onActivityResumed(Activity activity) {
L.e(activity.toString()+" onActivityResumed");
//Activity 跳转过程中,onPause -> onResume 需要时间,这过程中算前台?后台?
isForground = true;
timestamp = 0l;
isScreenOff = false;
}
@Override
public void onActivityPaused(Activity activity) {
L.e(activity.toString()+" onActivityPaused");
}
@Override
public void onActivityDestroyed(Activity activity) {
L.e(activity.toString()+" onActivityDestroyed");
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
}
private class DaemonReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
L.d("onReceive:" + action);
if (Intent.ACTION_SCREEN_OFF.equals(action)) {
AppStatusTracker.getInstance().onScreenOff(true);
}
}
}
}
使用:
public class CustomApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
AppStatusTracker.init(this);
}
}