package com.comdosoft.qxg;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import com.comdosoft.pojo.DownViewPojo;
import android.app.Activity;
import android.app.DownloadManager;
import android.app.DownloadManager.Request;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.MimeTypeMap;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
public class Qixueguan_appActivity extends Activity {
private static int MB_SIZE = 1024 * 1024;
private static int KB_SIZE = 1024;
private LinearLayout mLinearLayout;
private DownloadManager downloadManager;
private List<DownViewPojo> mDownViewList = new ArrayList<DownViewPojo>();
private List<String> mUrlList = new ArrayList<String>();
private List<Map<String, Object>> mData;
private Handler handler = new Handler() {
@Override
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;
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initData();
}
private void initData() {
downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
mLinearLayout = (LinearLayout) findViewById(R.id.mLinearLayout);
mUrlList.add("http://music.baidu.com/data/music/file?link=http://zhangmenshiting.baidu.com/data2/music/44804430/733833686400256.mp3?xcode=b6941d7fea0365339abd5bdf376d47f3959a2ceab6004476");
mUrlList.add("http://music.baidu.com/data/music/file?link=http://zhangmenshiting.baidu.com/data2/music/35468028/2355901379365261320.mp3?xcode=bf64667c54c02224b9ce27303f1db07269eae4df0fb501ff");
mUrlList.add("http://music.baidu.com/data/music/file?link=http://zhangmenshiting.baidu.com/data2/music/57129486/2658981379350861256.mp3?xcode=39cb9a27958605ddf9dd2bec60984cad0d620aa2c069e25d");
for (int i = 0; i < mUrlList.size(); i++) {
initView(i);
}
}
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);
}
public void downCourse(final int index) {
// 开始下载
Uri resource = Uri.parse(mUrlList.get(index));
DownloadManager.Request request = new DownloadManager.Request(resource);
request.setAllowedNetworkTypes(Request.NETWORK_MOBILE
| Request.NETWORK_WIFI);
request.setAllowedOverRoaming(false);
// 设置文件类型
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
String mimeString = mimeTypeMap.getMimeTypeFromExtension(MimeTypeMap
.getFileExtensionFromUrl(mUrlList.get(index)));
request.setMimeType(mimeString);
// 在通知栏中显示
request.setShowRunningNotification(false);
request.setVisibleInDownloadsUi(false);
// sdcard的目录下的download文件夹
request.setDestinationInExternalPublicDir("/download/", index + ".mp3");
final long id = downloadManager.enqueue(request);
Timer t = new Timer();
mDownViewList.get(index).setTimer(t);
t.schedule(new TimerTask() {
@Override
public void run() {
// 保存id
int[] bytesArr = getBytesAndStatus(id);
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);
}
public int[] getBytesAndStatus(long downloadId) {
int[] bytesAndStatus = new int[] { -1, -1, 0 };
DownloadManager.Query query = new DownloadManager.Query()
.setFilterById(downloadId);
Cursor c = null;
try {
c = downloadManager.query(query);
if (c != null && c.moveToFirst()) {
bytesAndStatus[0] = c
.getInt(c
.getColumnIndexOrThrow(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
bytesAndStatus[1] = c
.getInt(c
.getColumnIndexOrThrow(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
bytesAndStatus[2] = c.getInt(c
.getColumnIndex(DownloadManager.COLUMN_STATUS));
}
} finally {
if (c != null) {
c.close();
}
}
return bytesAndStatus;
}
public String getFileSize(long size) {
if (size <= 0) {
return "0M";
}
if (size >= MB_SIZE) {
return ((((double) (size * 100 / MB_SIZE)) / 100) + "M");
} else if (size >= KB_SIZE) {
return (((double) (size * 100 / KB_SIZE)) / 100 + "K");
} else {
return size + "B";
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Android 多线程下载+UI进度条刷新
共66个文件
class:40个
xml:8个
png:6个
5星 · 超过95%的资源 需积分: 12 46 下载量 161 浏览量
2013-09-17
14:44:51
上传
评论
收藏 434KB RAR 举报
温馨提示
http://blog.csdn.net/sky286753213/article/details/11770299
资源推荐
资源详情
资源评论
收起资源包目录
Download_Thread.rar (66个子文件)
Qixueguan_app
bin
classes.dex 346KB
res
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
ic_launcher.png 4KB
drawable-mdpi
ic_launcher.png 2KB
Qixueguan_app.apk 191KB
classes
com
comdosoft
pojo
DownViewPojo.class 2KB
qxg
Qixueguan_appActivity$2.class 878B
R$layout.class 475B
R$id.class 1KB
R$drawable.class 404B
Qixueguan_appActivity$3.class 1KB
R$attr.class 337B
QRcodeActivity$1.class 1KB
R.class 614B
Qixueguan_appActivity$1.class 2KB
R$string.class 424B
QRcodeActivity.class 2KB
R$color.class 1KB
Qixueguan_appActivity.class 7KB
R$raw.class 382B
zxing
camera
FlashlightManager.class 4KB
PreviewCallback.class 2KB
PlanarYUVLuminanceSource.class 3KB
AutoFocusCallback.class 2KB
CameraManager.class 6KB
CameraConfigurationManager.class 7KB
view
ViewfinderView.class 4KB
ViewfinderResultPointCallback.class 768B
activity
CaptureActivity.class 6KB
CaptureActivity$1.class 838B
decoding
InactivityTimer.class 2KB
Intents$Share.class 476B
Intents.class 619B
Intents$SearchBookContents.class 583B
Intents$Scan.class 849B
Intents$Encode.class 604B
InactivityTimer$DaemonThreadFactory.class 907B
DecodeHandler.class 4KB
CaptureActivityHandler.class 4KB
FinishListener.class 1KB
DecodeThread.class 2KB
DecodeFormatManager.class 4KB
CaptureActivityHandler$State.class 1KB
Intents$WifiConnect.class 583B
encoding
EncodingHandler.class 2KB
resources.ap_ 27KB
res
drawable-ldpi
ic_launcher.png 2KB
raw
beep.ogg 12KB
drawable-hdpi
ic_launcher.png 4KB
values
colors.xml 1KB
strings.xml 192B
ids.xml 1KB
drawable-mdpi
ic_launcher.png 3KB
layout
item.xml 1KB
qr.xml 395B
camera.xml 566B
main.xml 287B
assets
gen
com
comdosoft
qxg
R.java 4KB
proguard.cfg 1KB
.settings
org.eclipse.jdt.core.prefs 177B
src
com
comdosoft
pojo
DownViewPojo.java 1KB
qxg
Qixueguan_appActivity.java 6KB
.project 849B
.classpath 430B
project.properties 361B
AndroidManifest.xml 2KB
共 66 条
- 1
资源评论
- zjl54392014-03-17不错的例子,学习啦
- andy的逆袭历程2014-06-12很好啊,我自己做了一些噶东,很符合
- nianfanfan2015-08-28不错的例子,对我的开发很有帮助
- huyao80232017-10-31额,就是代码有点过时了
风无形_
- 粉丝: 21
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功