java Android - 检查应用程序是否可见(前景或背景)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java Android - 检查应用程序是否可见(前景或背景)相关的知识,希望对你有一定的参考价值。

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

import timber.log.Timber;

public class Foreground implements Application.ActivityLifecycleCallbacks {

    private static Foreground instance;
    private static int count = 0;

    public static void init(Application app) {
        if (instance == null) {
            instance = new Foreground();
            app.registerActivityLifecycleCallbacks(instance);

        }
    }

    public static Foreground get() {
        return instance;
    }

    public boolean isForeground() {
        return count > 0;
    }

    private Foreground() {
    }

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

    }

    @Override
    public void onActivityStarted(Activity activity) {
        count++;
        Timber.d("Activities Count - " + count);
    }

    @Override
    public void onActivityResumed(Activity activity) {

    }

    @Override
    public void onActivityPaused(Activity activity) {

    }

    @Override
    public void onActivityStopped(Activity activity) {
        count--;
        Timber.d("Activities Count - " + count);
    }

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

    }

    @Override
    public void onActivityDestroyed(Activity activity) {

    }

}

以上是关于java Android - 检查应用程序是否可见(前景或背景)的主要内容,如果未能解决你的问题,请参考以下文章

java Android - 检查是否在主线程上运行

java Android - 检查是否在主线程上运行

java Android - 检查是否在主线程上运行

java Android - 检查是否在主线程上运行

java Android - 检查是否在主线程上运行

java Android - 检查是否在主线程上运行