问题
I have a service which I start using
Intent serviceIntent = new Intent(getActivity().getApplicationContext(), LocationService.class);
getActivity().startService(serviceIntent);
When app is going into background, not closed, just not in foreground: i.e. press home button, I do:
Notification notification = mBuilder.build();
service.startForeground(NOTIFICATION, notification);
To bring the service to the foreground and show ongoing notification.
I have a BroadcastReceiver which is fired when the notification's shut down action is clicked (added action to notification).
The BroadcastReceiver stops the service:
@Override
public void onReceive(Context context, Intent intent) {
Intent serviceIntent = new Intent(context.getApplicationContext(), LocationService.class);
context.stopService(serviceIntent);
}
Now, there are two scenarios:
When app goes to background but not closed!, when pulling down the notification bar and clicking the shut down action -> the service's
onDestroyis called just fine. I can seeToastandLogmessages fromonDestroy.When app is closed (swiped away from apps list), meaning only service is running, when pulling down the notification bar and clicking the shut down action -> it doesn't look like the service's
onDestroyis called! The code over there is not executed, and I cant seeToastandLogmessages fromonDestroy.
However, it seems that the service does stop, because I can see the app removed from settings -> running apps after clicking the shut down.
So, is the service getting destroyed? and if so, why isn't the code in onDestroy gets executed?
How can I make sure onDestroy is called?
Obviously it has something to do with the fact that in "good" scenario, the app is still in the background, and in the "bad" scenario, the app is closed.
EDIT:
Thanks to CommonsWare's and John Leehey's comments, I understood that it could be that the "process is being terminated" and "there is no guarantee" that onDestory() will be called.
My onDestory looks like this:
@Override
public void onDestroy() {
super.onDestroy();
stopLocationUpdates();
googleApiClient.disconnect();
Foreground.get(this).removeListener(this);
// set default zoom
sharedPrefrencesManager.putFloat(getString(R.string.zoom_last_location_key), 15);
Log.i("test", "onDestroy");
}
Should I do the stuff like stopLocationUpdates(); and googleApiClient.disconnect(); in another place? since there is no guarantee of onDestory being called?
来源:https://stackoverflow.com/questions/39924379/not-sure-if-service-ondestroy-is-called-from-broadcastreceiver