package com.young.download.factory;
import java.io.File;
import java.net.URL;
import java.net.URLConnection;
import com.young.download.util.FileUtil;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
public class updateUIThread extends Thread{
private static final String TAG = "MultiThreadDownload";
/*** 文件大小 */
private int fileSize;
/** * 已经下载多少 */
private int downloadSize;
/**文件的url,线程编号,文件名称*/
private String UrlStr,ThreadNo,fileName;
/***保存的路径*/
private String savePath;
/**下载的百分比*/
private int downloadPercent = 0;
/**下载的 平均速度*/
private int downloadSpeed = 0;
/**下载用的时间*/
private int usedTime = 0;
/**开始时间*/
private long startTime;
/**当前时间*/
private long curTime;
/**是否已经下载完成*/
private boolean completed = false;
private Handler handler ;
/**
* 下载的构造函数
* @param url 请求下载的URL
* @param savePath 保存的路径
* @param fileName 保存的名字
*/
public updateUIThread(Handler handler,String url,String savePath,String fileName)
{
this.handler = handler;
this.UrlStr = url;
this.savePath = savePath;
this.fileName = fileName;
Log.e(TAG, toString());
}
@Override
public void run() {
try {
URL url = new URL(UrlStr);
URLConnection conn = url.openConnection();
fileSize = conn.getContentLength();
sendMsg(FileUtil.startDownloadMeg);
Log.e(TAG, "文件一共:"+fileSize);
File file = new File(savePath+fileName);
FileDownloadThread fdt = new FileDownloadThread(url, file);
fdt.setName("downloadThread");
fdt.start();
startTime = System.currentTimeMillis();
boolean finished = false;
while(!finished)
{
downloadSize = 0;
finished = true;
downloadSize+= fdt.getDownloadSize();
if(!fdt.isFinished())
{
finished = false;
}
downloadPercent = (downloadSize*100)/fileSize;
curTime = System.currentTimeMillis();
System.out.println("curTime = "+curTime+" downloadSize = "+downloadSize+" usedTime "+(int) ((curTime-startTime)/1000));
usedTime = (int) ((curTime-startTime)/1000);
if(usedTime==0){
usedTime = 1;
}
downloadSpeed = (downloadSize/usedTime)/1024;
sleep(1000);/*1秒钟刷新一次界面*/
sendMsg(FileUtil.updateDownloadMeg);
}
completed = true;
Log.e(TAG, "ok");
sendMsg(FileUtil.endDownloadMeg);
} catch (Exception e) {
Log.e(TAG, "multi file error Exception :"+e.getMessage());
e.printStackTrace();
}
super.run();
}
/**
* 得到文件的大小
* @return
*/
public int getFileSize()
{
return this.fileSize;
}
/**
* 得到已经下载的数量
* @return
*/
public int getDownloadSize()
{
return this.downloadSize;
}
/**
* 获取下载百分比
* @return
*/
public int getDownloadPercent(){
return this.downloadPercent;
}
/**
* 获取下载速度
* @return
*/
public int getDownloadSpeed(){
return this.downloadSpeed;
}
/**
* 分块下载完成的标志
* @return
*/
public boolean isCompleted(){
return this.completed;
}
@Override
public String toString() {
return "MultiThreadDownload [threadNum=" + ", fileSize="
+ fileSize + ", UrlStr=" + UrlStr + ", ThreadNo=" + ThreadNo
+ ", fileName=" + fileName + ", savePath=" + savePath + "]";
}
private void sendMsg(int what)
{
Message msg = new Message();
msg.what = what;
handler.sendMessage(msg);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
android_download单个下载线程.zip (40个子文件)
android_download单个下载线程
bin
androidDownloadTest.apk 20KB
classes.dex 12KB
res
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
ic_launcher.png 4KB
drawable-mdpi
ic_launcher.png 2KB
android_download.apk 19KB
androidDownloadSingleT.apk 19KB
jarlist.cache 119B
classes
com
young
download
MainActivity.class 3KB
BuildConfig.class 345B
MainActivity$1.class 3KB
R$layout.class 394B
R$id.class 410B
R$drawable.class 407B
factory
FileDownloadThread.class 3KB
updateUIThread.class 4KB
R$attr.class 340B
R.class 531B
R$string.class 427B
util
FileUtil.class 2KB
resources.ap_ 11KB
AndroidManifest.xml 981B
res
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
ic_launcher.png 4KB
values
strings.xml 186B
drawable-mdpi
ic_launcher.png 3KB
layout
main.xml 646B
assets
gen
com
young
download
BuildConfig.java 160B
R.java 790B
proguard.cfg 1KB
.settings
org.eclipse.core.resources.prefs 60B
org.eclipse.jdt.core.prefs 629B
src
com
young
download
factory
updateUIThread.java 4KB
FileDownloadThread.java 3KB
MainActivity.java 3KB
util
FileUtil.java 2KB
.project 858B
.classpath 364B
project.properties 360B
AndroidManifest.xml 981B
共 40 条
- 1
jiantaocd
- 粉丝: 10
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页