Victor Laerte
Victor Laerte

Reputation: 6556

Parallel Download with the same Intent Service

I have one Intent Service that process a download and unzip of some file. Now I'm trying to use this same Intent Service to download simultaneous files, but when I try to initiate the second download it just enqueue the new download, how can I not enqueue my downloads?

Intent service = new Intent(getActivity(), DownloadAndUnzipService.class);

service.putExtra("post", false);
service.putExtra("url", htmlUrlDownload);
service.putExtra("filePath", filePath);
service.putExtra("destinationPath", destinationPath);
service.putExtra("receiver", mReceiver);
service.putExtra("typeDownload", Constants.HTML);
service.putExtra("metaDado", downloadContent.getMetaDado());
service.putExtra("isResuming", false);

getActivity().startService(service);

I didn't put DownloadAndUnzipService code because it's too long and I think it won't help, but if is helpful I can update it.

Upvotes: 4

Views: 2711

Answers (2)

Richard Le Mesurier
Richard Le Mesurier

Reputation: 29762

IntentService is specifically designed to queue up multiple calls to it, and run them one at a time.

i.e. It is working 100% correctly, but unfortunately your design is wrong.

You should look into somthing like a ThreadPoolExecutor that will enable you to run multiple Runnables simultaneously from a thread pool. Check out the Android docs here.

You will also see some good sample code. Otherwise, I know Mark Murphy's ebooks have some great code for parallel downloading.

I'm not sure if you need to put that into an actual Service or if you can keep it in one of your Activities - that depends on your exact aims.

Upvotes: 11

Chiara
Chiara

Reputation: 1887

if DownloadAndUnzipService is a subclass of IntentService that is the expected behavior.

rea a little bit more at: Extends an Intent Service

If you wanna a parallel download, you would need to extend from Service class and manage the background thread by your self

Upvotes: 2

Related Questions