Checking if an Android application is running in the background

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

    I did my own implementation of ActivityLifecycleCallbacks. I'm using SherlockActivity, but for normal Activity class might work.

    First, I'm creating an interface that have all methods for track the activities lifecycle:

    public interface ActivityLifecycleCallbacks{
        public void onActivityStopped(Activity activity);
        public void onActivityStarted(Activity activity);
        public void onActivitySaveInstanceState(Activity activity, Bundle outState);
        public void onActivityResumed(Activity activity);
        public void onActivityPaused(Activity activity);
        public void onActivityDestroyed(Activity activity);
        public void onActivityCreated(Activity activity, Bundle savedInstanceState);
    }
    

    Second, I implemented this interface in my Application's class:

    public class MyApplication extends Application implements my.package.ActivityLifecycleCallbacks{
    
        @Override
        public void onCreate() {
            super.onCreate();           
        }
    
        @Override
        public void onActivityStopped(Activity activity) {
            Log.i("Tracking Activity Stopped", activity.getLocalClassName());
    
        }
    
        @Override
        public void onActivityStarted(Activity activity) {
            Log.i("Tracking Activity Started", activity.getLocalClassName());
    
        }
    
        @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());
        }
    
        @Override
        public void onActivityPaused(Activity activity) {
            Log.i("Tracking Activity Paused", activity.getLocalClassName());
        }
    
        @Override
        public void onActivityDestroyed(Activity activity) {
            Log.i("Tracking Activity Destroyed", activity.getLocalClassName());
        }
    
        @Override
        public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
            Log.i("Tracking Activity Created", activity.getLocalClassName());
        }
    }
    

    Third, I'm creating a class that extends from SherlockActivity:

    public class MySherlockActivity extends SherlockActivity {
    
        protected MyApplication nMyApplication;
    
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            nMyApplication = (MyApplication) getApplication();
            nMyApplication.onActivityCreated(this, savedInstanceState);
        }
    
        protected void onResume() {
            // TODO Auto-generated method stub
            nMyApplication.onActivityResumed(this);
            super.onResume();
    
        }
    
        @Override
        protected void onPause() {
            // TODO Auto-generated method stub
            nMyApplication.onActivityPaused(this);
            super.onPause();
        }
    
        @Override
        protected void onDestroy() {
            // TODO Auto-generated method stub
            nMyApplication.onActivityDestroyed(this);
            super.onDestroy();
        }
    
        @Override
        protected void onStart() {
            nMyApplication.onActivityStarted(this);
            super.onStart();
        }
    
        @Override
        protected void onStop() {
            nMyApplication.onActivityStopped(this);
            super.onStop();
        }
    
        @Override
        protected void onSaveInstanceState(Bundle outState) {
            nMyApplication.onActivitySaveInstanceState(this, outState);
            super.onSaveInstanceState(outState);
        }   
    }
    

    Fourth, all class that extend from SherlockActivity, I replaced for MySherlockActivity:

    public class MainActivity extends MySherlockActivity{
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
        }
    
    }
    

    Now, in the logcat you will see the logs programmed in the Interface implementation made in MyApplication.

提交回复
热议问题