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 线程 下载 APK 显示进度 下载速度
5星 · 超过95%的资源 需积分: 50 127 浏览量
2012-12-20
14:56:05
上传
评论 8
收藏 117KB ZIP 举报
jiantaocd
- 粉丝: 10
- 资源: 7
- 1
- 2
- 3
- 4
- 5
- 6
前往页