问题
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).
回答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(); } }
Make a custom 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) {} } }
回答2:
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
'sonResume()
/onPause()
to "make user online/offline"; use Service that sticks to the application meaning that if the app is killed after
Service
'sonStartCommand()
returns, the service will be recreated andonStartCommand()
will be called again. At this point you could "make user offline". The chain of lifecycle method calls would be:Activity
'sonStop()
->onDestroy()
* ->Service
'sonTaskRemoved()
* ->Application
'sonCreate()
->Service
'sonCreate()
->Service
'sonStartCommand()
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 runningActivity
instanceIntent
= null, meaning the request has been sent by the (newly created)Application
instance
* Not guaranteed to be called
回答3:
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.
来源:https://stackoverflow.com/questions/35729654/which-function-is-called-when-application-is-removed-from-task-manager