Question

I have an IntentService that uploads pictures and text to a server. I am launching a service with:

Intent intent = new Intent(this, MyIntentService.class);
startService(intent);

In the service, at various points within onHandleIntent(Intent intent), I am calling stopSelf() to terminate the service. I want to know when the service has stopped and handle changes in the UI when it has stopped. An additional caveat is that the user may be in a different activity than they were when the service started.

How can I listen for when the service is stopped?

Was it helpful?

Solution

I am calling stopSelf() to terminate the service

Why? If you want to skip over logic, that's fine, but please allow onHandleIntent() to finish normally and call stopSelf() on its own accord. For example, there may be more Intent commands queued up to be processed.

How can I listen for when the service is stopped?

Use an event bus, such as LocalBroadcastManager or greenrobot's EventBus.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top