Download files in queue in Android

前端 未结 3 1231
再見小時候
再見小時候 2021-01-01 08:05

How do I download multiple files in a queue one by one! I\'m using this as a sample code, since. I would be passing the URLs to download in Strings from my local DB dynamic

3条回答
  •  醉话见心
    2021-01-01 08:39

    From API 11 up, a good approach is to use a FixedThreadPool with async tasks. Do once:

    ExecutorService threadPoolExecutor = Executors.newFixedThreadPool(3);
    

    Where 3 is the number of downloads you want to run at the same time. It will queueu the task if there are already 3 downloads running, and automatically handle the task later. Launch your async tasks with:

    yourAsynTask.executeOnExecutor(threadPoolExecutor, params);
    

    Params is probably the url you wish to connect to. You can read it out in the onPostExecute of your asynctask, and connect to the server using a HttpURLConnection.

    Make sure you call down this on shutdown:

    threadPoolExecutor.shutdown()
    

提交回复
热议问题