美文网首页
监听app前后台工具类

监听app前后台工具类

作者: sollian | 来源:发表于2018-07-04 17:34 被阅读9次

AppBackgroundObservable

public final class AppBackgroundObservable implements Application.ActivityLifecycleCallbacks {
    private static final String TAG = AppBackgroundObservable.class.getSimpleName();

    private static final int MSG_BACK = 0;
    private static final int MSG_FORE = 1;

    private boolean isBackground;

    private final Collection<AppBackgroundObserver> appBackgroundObservers = new HashSet<>();

    private final Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_BACK:
                    for (AppBackgroundObserver appBackgroundObserver : appBackgroundObservers) {
                        appBackgroundObserver.onAppBackground();
                    }
                    isBackground = true;
                    Log.d(TAG, "app in the background");
                    break;
                case MSG_FORE:
                    for (AppBackgroundObserver appBackgroundObserver : appBackgroundObservers) {
                        appBackgroundObserver.onAppForeground();
                    }
                    isBackground = false;
                    Log.d(TAG, "app in the foreground");
                    break;
                default:
                    break;
            }
            return true;
        }
    });

    public static final AppBackgroundObservable INSTANCE = new AppBackgroundObservable();

    private AppBackgroundObservable() {
    }

    /**
     * {@link Application#onCreate()}中调用。
     * 如果有多个进程,请判断是否是应用程序的主要进程
     *
     * @param application
     */
    public void init(Application application) {
        application.unregisterActivityLifecycleCallbacks(this);
        application.registerActivityLifecycleCallbacks(this);
    }

    public void addAppBackgroundListener(AppBackgroundObserver listener) {
        if (listener != null) {
            appBackgroundObservers.add(listener);
        }
    }

    public void removeAppBackgroundListener(AppBackgroundObserver listener) {
        if (listener != null) {
            appBackgroundObservers.remove(listener);
        }
    }

    public boolean isBackground() {
        return isBackground;
    }

    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
    }

    @Override
    public void onActivityStarted(Activity activity) {
    }

    @Override
    public void onActivityResumed(Activity activity) {
        if (handler.hasMessages(MSG_BACK)) {
            handler.removeMessages(MSG_BACK);
        } else {
            handler.sendEmptyMessage(MSG_FORE);
        }
    }

    @Override
    public void onActivityPaused(Activity activity) {
        handler.removeMessages(MSG_BACK);
        handler.sendEmptyMessageDelayed(MSG_BACK, 300);
    }

    @Override
    public void onActivityStopped(Activity activity) {
    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
    }

    @Override
    public void onActivityDestroyed(Activity activity) {
    }
}

AppBackgroundObserver

public interface AppBackgroundObserver {
    void onAppBackground();

    void onAppForeground();
}

相关文章

网友评论

      本文标题:监听app前后台工具类

      本文链接:https://www.haomeiwen.com/subject/gjcyuftx.html