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

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

问题描述

我正在为Android编写一个应用程序,让用户浏览文件列表并下载它们。
对于每次下载,我创建了一个线程,我使用HttpURLConnection实例下载文件(通过在while循环中从连接中读取)。



此方法一个主动下载工作正常。但是当用户启动多个时,下载性能会显着下降。大多数情况下,这些并行下载消耗所有带宽,用户无法浏览文件(使用另一个HttpUrlConnection加载文件列表)。



任何建议完善下载系统?
谢谢。



P.S .:流行的浏览器(如Google Chrome和Firefox)的方法似乎很好。任何人都知道他们如何工作?

解决方案

唉,我不知道有什么办法来限制某些连接。然而,实际的方法是实现下载队列来控制同时下载的次数。在你的情况下,你可能只想让一次下载1件事情。这可以通过几种不同的方式实现。



这是一种处理程序和Looper的方法: http://mindtherobot.com/blog/159/android-guts-intro-to-loopers-and-handlers/



编辑1:
请看老鼠的评论。一次最多可以下载2个线程可能更智能。


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?

解决方案

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.

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

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