代码 http://download.csdn.net/detail/sky286753213/6281323
实现单个任务的下载和UI刷新很简单,而要实现多个任务下载和刷新UI就相对复杂一些了
下面谈一谈自己的思路,首先动态添加每个下载项
这里我对下载项做了一个布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:text="0M/0M"
android:id="@+id/downSize"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="28dp" />
<TextView
android:text="0%"
android:id="@+id/downPro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textSize="28dp" />
<ProgressBar
android:id="@+id/downPb"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="10dp"
android:layout_below="@id/downSize" />
<Button
android:id="@+id/downBn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/downPb"
android:text="下载" />
</RelativeLayout>
其实就是文件大小,百分比,进度条和按钮
然后创建一个JavaBean 用来装载每个下载项的组件
private TextView pro;
private TextView size;
private ProgressBar pb;
private Button bn;
private Timer timer;
timer用处下面在做解释
下面就是对JavaBean进行绑定和组件的初始化了
public void initView(final int index) {
LayoutInflater inflater = LayoutInflater.from(this);
RelativeLayout relativeLayout = (RelativeLayout) inflater.inflate(
R.layout.item, null);
TextView pro = (TextView) relativeLayout.findViewById(R.id.downPro);
TextView size = (TextView) relativeLayout.findViewById(R.id.downSize);
ProgressBar pb = (ProgressBar) relativeLayout.findViewById(R.id.downPb);
Button bn = (Button) relativeLayout.findViewById(R.id.downBn);
DownViewPojo dv = new DownViewPojo(pro, size, pb, bn);
mDownViewList.add(dv);
bn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
downCourse(index);
}
});
mLinearLayout.addView(relativeLayout);
}
这里用了一个List来装载每个JavaBean
下面是点击事件的处理
Timer t = new Timer();
mDownViewList.get(index).setTimer(t);
t.schedule(new TimerTask() {
@Override
public void run() {
Bundle bundle = new Bundle();
bundle.putInt("index", index);
Message msg = handler.obtainMessage(1, bytesArr[0],
bytesArr[1], bytesArr[2]);
msg.setData(bundle);
handler.sendMessage(msg);
};
}, 0, 1000);
bytesArr是使用DownloadManager类获取的文件信息 [1]总大小,[0]文件当前大小
handler进行UI刷新
public void handleMessage(Message msg) {
super.handleMessage(msg);
int msgId = msg.what;
switch (msgId) {
case 1:
DownViewPojo dv = mDownViewList.get(msg.getData().getInt(
"index"));
dv.getPb().setMax(msg.arg2);
dv.getPb().setProgress(msg.arg1);
dv.getSize().setText(
getFileSize(msg.arg1) + "/" + getFileSize(msg.arg2));
dv.getPro().setText(msg.arg1 * 100 / msg.arg2 + "%");
if (msg.arg1 == msg.arg2) {
Toast.makeText(getApplicationContext(),
"文件" + msg.getData().getInt("index") + "下载完成", 0)
.show();
dv.getBn().setText("已下载");
dv.getTimer().cancel();
}
break;
}
}
timer是每秒刷新一次 每次线程刷新时更新相应的UI
从List中获取对应的JavaBean 对其进行更新操作
最后进行判断当前文件大小==文件总大小则timer.cancel()