johan
johan

Reputation: 6656

Several AsyncTasks in Service

I have a service which can be called from Activities in an app.

The service contains of two different AsyncTasks that downloads data and populates a DB. The tasks downloads different data, and the operations may take some time. So I want to prevent the Tasks from being run simultaneously, but if one task is running I would like to queue the second task.

This could happen if the users switch activity before the task is completed.

How can this be done? Can I sleep the second task in some way? Hope this is understandable :)

Thanks guys!

Upvotes: 0

Views: 65

Answers (1)

Graham Borland
Graham Borland

Reputation: 60681

Would an IntentService be more appropriate? You can fire tasks at it, and it'll execute them one at a time in a single background thread.

If you need to get results back from these operations, you can use a BroadcastReceiver to capture messages from the service.

Upvotes: 2

Related Questions