Checking if an Android application is running in the background

前端 未结 30 2838
无人共我
无人共我 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 06:51

    Building on @Cornstalks answer to include a couple of useful features.

    Extra features:

    • introduced singleton pattern so you can do this anywhere in the application: AppLifecycleHandler.isApplicationVisible() and AppLifecycleHandler.isApplicationInForeground()
    • added handling of duplicate events (see comments // take some action on change of visibility and // take some action on change of in foreground)

    App.java

    public class App extends Application {
        @Override
        public void onCreate() {
            super.onCreate();
    
            registerActivityLifecycleCallbacks(AppLifecycleHandler.getInstance());
        }
    }
    

    AppLifecycleHandler.java

    public class AppLifecycleHandler implements Application.ActivityLifecycleCallbacks {
        private int resumed;
        private int started;
    
        private final String DebugName = "AppLifecycleHandler";
    
        private boolean isVisible = false;
        private boolean isInForeground = false;
    
        private static AppLifecycleHandler instance;
    
        public static AppLifecycleHandler getInstance() {
            if (instance == null) {
                instance = new AppLifecycleHandler();
            }
    
            return instance;
        }
    
        private AppLifecycleHandler() {
        }
    
        @Override
        public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        }
    
        @Override
        public void onActivityDestroyed(Activity activity) {
        }
    
        @Override
        public void onActivityResumed(Activity activity) {
            ++resumed;
            android.util.Log.w(DebugName, "onActivityResumed -> application is in foreground: " + (resumed > 0) + " (" + activity.getClass() + ")");
            setForeground((resumed > 0));
        }
    
        @Override
        public void onActivityPaused(Activity activity) {
            --resumed;
            android.util.Log.w(DebugName, "onActivityPaused -> application is in foreground: " + (resumed > 0) + " (" + activity.getClass() + ")");
            setForeground((resumed > 0));
        }
    
        @Override
        public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
        }
    
        @Override
        public void onActivityStarted(Activity activity) {
            ++started;
            android.util.Log.w(DebugName, "onActivityStarted -> application is visible: " + (started > 0) + " (" + activity.getClass() + ")");
            setVisible((started > 0));
        }
    
        @Override
        public void onActivityStopped(Activity activity) {
            --started;
            android.util.Log.w(DebugName, "onActivityStopped -> application is visible: " + (started > 0) + " (" + activity.getClass() + ")");
            setVisible((started > 0));
        }
    
        private void setVisible(boolean visible) {
            if (isVisible == visible) {
                // no change
                return;
            }
    
            // visibility changed
            isVisible = visible;
            android.util.Log.w(DebugName, "App Visiblility Changed -> application is visible: " + isVisible);
    
            // take some action on change of visibility
        }
    
        private void setForeground(boolean inForeground) {
            if (isInForeground == inForeground) {
                // no change
                return;
            }
    
            // in foreground changed
            isInForeground = inForeground;
            android.util.Log.w(DebugName, "App In Foreground Changed -> application is in foreground: " + isInForeground);
    
            // take some action on change of in foreground
    
        }
    
        public static boolean isApplicationVisible() {
            return AppLifecycleHandler.getInstance().started > 0;
        }
    
        public static boolean isApplicationInForeground() {
            return AppLifecycleHandler.getInstance().resumed > 0;
        }
    }
    

提交回复
热议问题