Checking if an Android application is running in the background

前端 未结 30 2806
无人共我
无人共我 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:18

    Another solution for this old post (for those that it might help) :


    
    

    public class BaseApplication extends Application {
    
        private class Status {
            public boolean isVisible = true;
            public boolean isFocused = true;
        }
    
        private Map activities;
    
        @Override
        public void onCreate() {
            activities = new HashMap();
            super.onCreate();
        }
    
        private boolean hasVisibleActivity() {
            for (Status status : activities.values())
                if (status.isVisible)
                    return true;
            return false;
        }
    
        private boolean hasFocusedActivity() {
            for (Status status : activities.values())
                if (status.isFocused)
                    return true;
            return false;
        }
    
        public void onActivityCreate(Activity activity, boolean isStarting) {
            if (isStarting && activities.isEmpty())
                onApplicationStart();
            activities.put(activity, new Status());
        }
    
        public void onActivityStart(Activity activity) {
            if (!hasVisibleActivity() && !hasFocusedActivity())
                onApplicationForeground();
            activities.get(activity).isVisible = true;
        }
    
        public void onActivityWindowFocusChanged(Activity activity, boolean hasFocus) {
            activities.get(activity).isFocused = hasFocus;
        }
    
        public void onActivityStop(Activity activity, boolean isFinishing) {
            activities.get(activity).isVisible = false;
            if (!isFinishing && !hasVisibleActivity() && !hasFocusedActivity())
                onApplicationBackground();
        }
    
        public void onActivityDestroy(Activity activity, boolean isFinishing) {
            activities.remove(activity);
            if(isFinishing && activities.isEmpty())
                onApplicationStop();
        }
    
        private void onApplicationStart() {Log.i(null, "Start");}
        private void onApplicationBackground() {Log.i(null, "Background");}
        private void onApplicationForeground() {Log.i(null, "Foreground");}
        private void onApplicationStop() {Log.i(null, "Stop");}
    
    }
    

    public class MyActivity extends BaseActivity {...}
    

    public class BaseActivity extends Activity {
    
        private BaseApplication application;
    
        @Override
        protected void onCreate(Bundle state) {
            application = (BaseApplication) getApplication();
            application.onActivityCreate(this, state == null);
            super.onCreate(state);
        }
    
        @Override
        protected void onStart() {
            application.onActivityStart(this);
            super.onStart();
        }
    
        @Override
        public void onWindowFocusChanged(boolean hasFocus) {
            application.onActivityWindowFocusChanged(this, hasFocus);
            super.onWindowFocusChanged(hasFocus);
        }
    
        @Override
        protected void onStop() {
            application.onActivityStop(this, isFinishing());
            super.onStop();
        }
    
        @Override
        protected void onDestroy() {
            application.onActivityDestroy(this, isFinishing());
            super.onDestroy();
        }
    
    }
    

提交回复
热议问题