在Android应用程序同时下载多个文件 [英] Downloading multiple files simultaneously in Android applications

查看:139
本文介绍了在Android应用程序同时下载多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写这让用户浏览文件的列表,并下载他们的Andr​​oid应用程序。 对于每一个下载,我创建了一个线程,我下载用HttpURLConnection实例文件(通过从while循环连接读取)。

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).

此方法正常工作与一个主动下载。但是,当用户启动一个以上的,下载性能急剧下降。在大多数情况下,这些并行下载消耗所有的带宽和用户不能浏览文件(使用另一个HttpURLConnection类加载的文件列表)。

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.

PS:那个流行的浏览器,如谷歌Chrome和Firefox做的方法似乎不错。任何人都知道他们是如何工作的?

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

推荐答案

唉,我不知道的方法阻止某些连接。然而,实用的方法是执行下载的队列控制同时下载的数量。在你的情况,你可能会想只让1东西下载的时间。这可以实现几种不同的方式。

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.

下面是一个方式与处理程序做出来,一个活套:的 http://mindtherobot.com/blog/159/android-guts-intro-to-loopers-and-handlers/

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

编辑1: 见老鼠的评论。这可能是聪明有最多2线程下载的时间。

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

这篇关于在Android应用程序同时下载多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆