Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parallel Download with the same Intent Service

Tags:

android

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.

like image 609
Victor Laerte Avatar asked Oct 20 '25 12:10

Victor Laerte


2 Answers

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.

like image 57
Richard Le Mesurier Avatar answered Oct 23 '25 01:10

Richard Le Mesurier


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

like image 21
Chiara Avatar answered Oct 23 '25 03:10

Chiara



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!