How to detect when an Android app goes to the background and come back to the foreground

后端 未结 30 1323
独厮守ぢ
独厮守ぢ 2020-11-22 00:56

I am trying to write an app that does something specific when it is brought back to the foreground after some amount of time. Is there a way to detect when an app is sent to

30条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 01:19

    You can achieve this easily with the help of ActivityLifecycleCallbacks and ComponentCallbacks2 something like below.

    Create a class AppLifeCycleHandler implementing above said interfaces.

    package com.sample.app;
    
    import android.app.Activity;
    import android.app.Application;
    import android.content.ComponentCallbacks2;
    import android.content.res.Configuration;
    import android.os.Bundle;
    
    /**
     * Created by Naveen on 17/04/18
     */
    public class AppLifeCycleHandler
        implements Application.ActivityLifecycleCallbacks, ComponentCallbacks2 {
    
      AppLifeCycleCallback appLifeCycleCallback;
    
      boolean appInForeground;
    
      public AppLifeCycleHandler(AppLifeCycleCallback appLifeCycleCallback) {
        this.appLifeCycleCallback = appLifeCycleCallback;
      }
    
      @Override
      public void onActivityResumed(Activity activity) {
        if (!appInForeground) {
          appInForeground = true;
          appLifeCycleCallback.onAppForeground();
        }
      }
    
      @Override
      public void onTrimMemory(int i) {
        if (i == ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
          appInForeground = false;
          appLifeCycleCallback.onAppBackground();
        }
      }
    
      @Override
      public void onActivityCreated(Activity activity, Bundle bundle) {
    
      }
    
      @Override
      public void onActivityStarted(Activity activity) {
    
      }
    
      @Override
      public void onActivityPaused(Activity activity) {
    
      }
    
      @Override
      public void onActivityStopped(Activity activity) {
    
      }
    
      @Override
      public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
    
      }
    
      @Override
      public void onActivityDestroyed(Activity activity) {
    
      }
    
      @Override
      public void onConfigurationChanged(Configuration configuration) {
    
      }
    
      @Override
      public void onLowMemory() {
    
      }
    
      interface AppLifeCycleCallback {
    
        void onAppBackground();
    
        void onAppForeground();
      }
    }
    

    In your class which extends Application implement AppLifeCycleCallback to get the callbacks when app switches between foreground and background. Something like below.

    public class BaseApplication extends Application implements AppLifeCycleHandler.AppLifeCycleCallback{
    
        @Override
        public void onCreate() {
            super.onCreate();
            AppLifeCycleHandler appLifeCycleHandler = new AppLifeCycleHandler(this);
            registerActivityLifecycleCallbacks(appLifeCycleHandler);
            registerComponentCallbacks(appLifeCycleHandler);
        }
    
        @Override
        public void onAppBackground() {
            Log.d("LifecycleEvent", "onAppBackground");
        }
    
        @Override
        public void onAppForeground() {
            Log.d("LifecycleEvent", "onAppForeground");
        }
    }
    

    Hope this helps.

    EDIT As an alternative you can now use Life cycle aware architecture component.

提交回复
热议问题