Android的下载多个文件,并显示在ListView的进步 [英] Android Download Multiple Files and show progress in ListView

查看:298
本文介绍了Android的下载多个文件,并显示在ListView的进步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有N个文件的下载。我想以串行下载他们的背景 - 另一个之后。我想显示在ListView的进度和状态。因此,ListView的是这样的:

I have N files to download. I want to download them serially in background - one after other. I want to show the progress and status in a ListView. So, ListView looks like:

文件1 ...下载

文件2 ....下载

FILE3 ...进行中说,39%

File3... in Progress say 39%

FILE4 ...挂起

File4... Pending

...

列表视图定期刷新(比如每2秒),文件比较大:50 MB左右。因此,他们拿1分加下载。

Listview refreshed periodically (say every 2 seconds) and files are large: 50 MB or so. So they take 1 minute plus to download.

我将传递文件的URL到AsyncTask的名单,并让它在后台下载。问题是,你怎么养活进步回的ListView。

I will pass a list of File URLs to the AsyncTask and have it downloaded in background. Question is how do you feed progress back into ListView.

考虑到用户可以从ListView的页面导航离开留下的AsyncTask只跑回来查看下载状态。

Consider that user may navigate away from ListView page leaving AsyncTask running only to come back to see the status of download.

那么,如何在ListView观察的AsyncTask的进展,并显示呢?

So how does the ListView "observe" the progress of AsyncTask and display it ?

我想添加一组静态变量的AsyncTask(保护他们同步块),并利用内部片段的定期更新的用户界面,显示的ListView。因此,该片段将是这样的:

I am thinking adding a set of static variables AsyncTask (protecting them with "Synchronized" blocks) and the use a periodic UI Updater inside Fragment that displays the ListView. So the fragment will have something like this:

重复具有时间延迟一个任务

有没有一种设计模式,公共项目或样品code这个?

Is there a design pattern, public project or sample code for this ?

对不起,我不AVE一张好照片。最接近我所描述的是在这里(类似但我打算使用的AsyncTask和串行下载):

Sorry, I don't ave a good picture. Closest thing to what I am describing is here (Similar but I plan to use AsyncTask and serial download):

推荐答案

请按照下列步骤

  1. 在它下载的文件创建的AsyncTask服务
  2. 启动任务服务的onStartCommand
  3. 注册广播reciever在您的活动才能收到来自服务的进展
  4. 的AsyncTask的onProgressUpdate发送广播给你activiy
  5. 显示在ListView
  6. 进度

这篇关于Android的下载多个文件,并显示在ListView的进步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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