Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Downloading multiple files simultaneously in Android applications

I'm writing an application for Android which let users browse a list of files and download them. For every download, I created a thread and I download the file with an HttpURLConnection instance (by reading from the connection in a while loop).

This method works fine with one active download. But when user starts more than one, download performance degrades dramatically. Most of the time, these parallel downloads consume all the bandwidth and the users is unable to browse files (which uses another HttpUrlConnection to load the files list).

Any suggestions on refining the download system? Thanks.

P.S.: The method that popular browsers such as Google Chrome and Firefox do seems good. Anyone knows how they work?

like image 575
Sadjad Avatar asked Sep 27 '11 05:09

Sadjad


People also ask

How can I download multiple files at once on Android?

If user click music item, I add an downloading item to second listview and start download file with AsyncTask. User can click more than one music, it means downloading multiple files simultaneously. I can download files with AsyncTask.

How do I download multiple files at once?

Hold CTRL and click on the files you want to download. Once you have selected the files you want, right click on the last file you selected and select download.

Is it faster to download multiple things at once or one at a time?

Every additional file download increases the average speed and decreases the total time to completion. Six simultaneous downloads of roughly the same size can complete in 1/6 the elapsed time of a single download, if they all complete within a few seconds of each other.

Are parallel downloads faster?

Parallel downloads, also known as domain sharding, reduce the time it takes for a connection to be complete which results in faster page loading and download speeds.


1 Answers

Alas, i don't know of a way to throttle certain connections. However, a practical approach would be to implement a queue of downloads to control the number of simultaneous downloads. In your case, you would probably want to only let 1 thing download at a time. This can be implemented a few different ways.

Here's a way to do it with Handlers and a Looper: http://mindtherobot.com/blog/159/android-guts-intro-to-loopers-and-handlers/

Edit 1: See mice's comment. It may be smarter to have a max of 2 threads downloading at a time.

like image 53
Ian Avatar answered Nov 01 '22 01:11

Ian