Which function is called when application is remov

2020-02-15 03:46发布

I need to make status of user offline. When I press home button onStop() is called, that's fine. When I press back button onDestroy() is invoked. But when I close the app from recent apps by swiping it, onStop() or onDestroy() isn't called.

I need to know when the app is closed from recent apps to do something (e.g make user offline).

3条回答
在下西门庆
2楼-- · 2020-02-15 04:00
  1. Make a service :

    public class MyService extends Service {
    private DefaultBinder mBinder;
    private AlarmManager  alarmManager ;
    private PendingIntent alarmIntent;
    
    private void setAlarmIntent(PendingIntent alarmIntent){
    this.alarmIntent=alarmIntent;
    }
    
    public void onCreate() {
    alarmManager (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    mBinder = new DefaultBinder(this);
    }
    
     @Override
     public IBinder onBind(Intent intent) {
      return mBinder;
     }
    
     public void onTaskRemoved (Intent rootIntent){
     alarmManager.cancel(alarmIntent);
     this.stopSelf();
    }
    }
    
  2. Make a custom class :

    public class DefaultBinder extends Binder {
        MyService s;
    
        public DefaultBinder( MyService s) {
            this.s = s;
        }
    
        public MyService getService() {
            return s;
        }
    }
    
  3. Add to your activity :

    MyService service;
    protected ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder binder) {
    service = ((DefaultBinder) binder).getService();
    service.setAlarmIntent(pIntent);
        }
    
        public void onServiceDisconnected(ComponentName className) {
            service = null;
        }
            };
    
    protected void onResume() {
        super.onResume();
        bindService(new Intent(this, MainService.class), mConnection,
                Context.BIND_AUTO_CREATE);
    }
    
    @Override
    protected void onStop() {
        super.onStop();
    
        if (mConnection != null) {
            try {
                unbindService(mConnection);
            } catch (Exception e) {}
        }
    }
    
查看更多
手持菜刀,她持情操
3楼-- · 2020-02-15 04:11

But when I close the app from recent apps by swiping it, onStop() or onDestroy() isn't called.

Methods of Activity lifecycle that are to be called when Activity is no longer visible are not guaranteed to be called when removed from the recent tasks (treat it as "soft" version of killing an app by the system due to low memory).

I need to know when the app is closed from recent apps to do something (e.g make user offline)

I suggest one of the following:

  • (If applicable) use Activity's onResume()/onPause() to "make user online/offline";
  • use Service that sticks to the application meaning that if the app is killed after Service's onStartCommand() returns, the service will be recreated and onStartCommand() will be called again. At this point you could "make user offline". The chain of lifecycle method calls would be:

    1. Activity's onStop() -> onDestroy()* ->
    2. Service's onTaskRemoved()* ->
    3. Application's onCreate() -> Service's onCreate() ->
    4. Service's onStartCommand()

The Intent passed to the method will help you recognize which component triggered the start request:

  • Intent != null, meaning the request has been received from a running Activity instance
  • Intent = null, meaning the request has been sent by the (newly created) Application instance

* Not guaranteed to be called

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2020-02-15 04:22

No, there is no clean way to get the application termination time. But I could suggest you a dirty trick to use a service to update your application (offline functionalities) for every after n minutes.

When OS kill your application, it will remove all associated services and broadcast receiver along with it.

查看更多
登录 后发表回答