What function is called when an application is removed from the task manager

I need to make user status offline. When I press the home button onStop(), this is normal. When I press the button onDestroy(), it is called. But when I close the application from the latest applications, scrolling it, onStop()or onDestroy()not called.

I need to know when the application is closed from the latest applications in order to do something (for example, disconnect the user).

+1
source share
3 answers
  • 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();
    }
    }
    
  • Create your own class:

    public class DefaultBinder extends Binder {
        MyService s;
    
        public DefaultBinder( MyService s) {
            this.s = s;
        }
    
        public MyService getService() {
            return s;
        }
    }
    
  • 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
source

, , onStop() onDestroy() .

Activity , , Activity , , ( "" - ).

, , - (, )

:

  • ( ) Activity onResume()/onPause(), " /";
  • Service, sticks , , Service onStartCommand(), , onStartCommand() . " ". :

    • Activity onStop() → onDestroy() * →
    • Service onTaskRemoved() * →
    • Application onCreate() → Service onCreate() →
    • Service onStartCommand()

Intent, , , :

  • Intent!= null, Activity
  • Intent= null, () Application

*

+1

No, there is no clean way to get application shutdown time. But I could offer you a dirty trick to use the service to update your application (stand-alone functions) for every n minutes.

When the OS kills your application, it removes all associated services and the broadcast receiver along with it.

0
source

Source: https://habr.com/ru/post/1666690/


All Articles