Question

I am working on a applicatin that syncs with videos on server. It starts a IntentService that downloads playlist, inserts it into database and than downloads actual video files to sd-card.

I am using a messenger passed via intent when starting the service to communicate back how much % is already downloaded.

I works but obviously when activity gets destroyed and starts back again, the messenger doesnt reconnect.

What would be the solution to this problem, steer me into correct direction. Thanks

Was it helpful?

Solution

If you want to use a messenger, you need to keep a reference to the handler created with the messenger that you passed to the intent service.

If the activity gets destroyed and recreated, that handler is still bound to the previous activity (which may also result in a leak).

One approach may be to have a reference to that handler inside a singleton object or inside the application object.

However, because of the asynchronous nature of intentservices, what I would do is notify the progress through broadcasts, so new activities may register to the same broadcast thrown by the intentservice. You never know if the service is still alive or has finished its job.

Using localbroadcasts may result in a more efficient solution.

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