Checking if an Android application is running in the background

前端 未结 30 2788
无人共我
无人共我 2020-11-21 06:19

By background, I mean none of the application\'s activities are currently visible to the user?

相关标签:
30条回答
  • 2020-11-21 07:11

    What about using getApplicationState().isInForeground() ?

    0 讨论(0)
  • 2020-11-21 07:12

    I recommend reading through this page: http://developer.android.com/reference/android/app/Activity.html

    In short, your activity is no longer visible after onStop() has been called.

    0 讨论(0)
  • 2020-11-21 07:13

    The only one correct solution:

    MainActivity.java:

    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            MyApp.mainActivity = this;
            super.onCreate(savedInstanceState);
            ...
        }
    

    MyApp.java:

    public class MyApp extends Application implements LifecycleObserver {
    
        public static MainActivity mainActivity = null;
    
        @Override
        public void onCreate() {
            super.onCreate();
            ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
        }
    
        @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
        void onAppBackgrounded() {
            // app in background
            if (mainActivity != null) {
                ...
            }
        }
    
        @OnLifecycleEvent(Lifecycle.Event.ON_START)
        void onAppForegrounded() {
            // app in foreground
            if (mainActivity != null) {
                ...
            }
        }
    
    }
    
    0 讨论(0)
  • 2020-11-21 07:13

    Since it isn't already mentioned, I will suggest the readers to explore ProcessLifecycleOwner available through Android Architecture components

    0 讨论(0)
  • 2020-11-21 07:13

    See the comment in the onActivityDestroyed function.

    Works with SDK target version 14> :

    import android.app.Activity;
    import android.app.Application;
    import android.os.Bundle;
    import android.util.Log;
    
    public class AppLifecycleHandler implements Application.ActivityLifecycleCallbacks {
    
        public static int active = 0;
    
        @Override
        public void onActivityStopped(Activity activity) {
            Log.i("Tracking Activity Stopped", activity.getLocalClassName());
            active--;
        }
    
        @Override
        public void onActivityStarted(Activity activity) {
            Log.i("Tracking Activity Started", activity.getLocalClassName());
            active++;
        }
    
        @Override
        public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
            Log.i("Tracking Activity SaveInstanceState", activity.getLocalClassName());
        }
    
        @Override
        public void onActivityResumed(Activity activity) {
            Log.i("Tracking Activity Resumed", activity.getLocalClassName());
            active++;
        }
    
        @Override
        public void onActivityPaused(Activity activity) {
            Log.i("Tracking Activity Paused", activity.getLocalClassName());
            active--;
        }
    
        @Override
        public void onActivityDestroyed(Activity activity) {
            Log.i("Tracking Activity Destroyed", activity.getLocalClassName());
            active--;
    
            // if active var here ever becomes zero, the app is closed or in background
            if(active == 0){
                ...
            }
    
        }
    
        @Override
        public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
            Log.i("Tracking Activity Created", activity.getLocalClassName());
            active++;
        }
    }
    
    0 讨论(0)
  • 2020-11-21 07:14

    Activity gets paused when a Dialog comes above it so all the recommended solutions are half-solutions. You need to create hooks for dialogs as well.

    0 讨论(0)
提交回复
热议问题