java 检查Android App的后台/前台状态

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 检查Android App的后台/前台状态相关的知识,希望对你有一定的参考价值。

import android.app.Activity;
import android.app.Application;
import android.content.ComponentCallbacks2;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;

/**
 * Created by Fabrizio on 13/11/17.
 */

public class ApplicationLifecycleHandler implements Application.ActivityLifecycleCallbacks, ComponentCallbacks2 {

    private final String TAG = ApplicationLifecycleHandler.class.getSimpleName();
    private boolean isInForeground = false;
    private int numActivities = 0;

    private ApplicationLifecycleHandlerListener listener;

    public ApplicationLifecycleHandler(ApplicationLifecycleHandlerListener listenerInput)
    {
        super();

        this.listener = listenerInput;
    }

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

    @Override
    public void onActivityStarted(Activity activity) {
        if (numActivities == 0) {
            // app went to foreground
            Log.d(TAG, "app went to foreground");
            isInForeground = true;

            if(this.listener!=null)
            {
                this.listener.onApplicationLifecycleForegroundStatusChanged(isInForeground);
            }
        }
        numActivities++;
    }

    @Override
    public void onActivityResumed(Activity activity) {

    }

    @Override
    public void onActivityPaused(Activity activity) {
    }

    @Override
    public void onActivityStopped(Activity activity) {
        numActivities--;
        if (numActivities == 0) {
            // app went to foreground
            Log.d(TAG, "app went to background");
            isInForeground = false;

            if(this.listener!=null)
            {
                this.listener.onApplicationLifecycleForegroundStatusChanged(isInForeground);
            }
        }
    }

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

    @Override
    public void onActivityDestroyed(Activity activity) {
    }

    @Override
    public void onConfigurationChanged(Configuration configuration) {
    }

    @Override
    public void onLowMemory() {
    }

    @Override
    public void onTrimMemory(int i) {
        if(i == ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN){
            Log.d(TAG, "app went to background");
            isInForeground = false;
            numActivities = 0;

            if(this.listener!=null)
            {
                this.listener.onApplicationLifecycleForegroundStatusChanged(isInForeground);
            }

        }
    }

    public interface ApplicationLifecycleHandlerListener
    {
        public void onApplicationLifecycleForegroundStatusChanged(boolean isInForeground);
    }
}

以上是关于java 检查Android App的后台/前台状态的主要内容,如果未能解决你的问题,请参考以下文章

检查应用程序是不是正在运行(后台或前台)Android

Android 监听APP进入后台或切换到前台方案对比

如何检查Android应用程序是在后台还是前台或终止?

Android app后台回到前台时的监听

Android开发检测App从后台进入前台的解决方法

android 判断app是否处于前台