Android多線程斷點下載的代碼流程解析:
運行效果圖:

實現(xiàn)流程全解析:
Step 1:創(chuàng)建一個用來記錄線程下載信息的表
創(chuàng)建數(shù)據(jù)庫表,于是乎我們創(chuàng)建一個數(shù)據(jù)庫的管理器類,繼承SQLiteOpenHelper類 重寫onCreate()與onUpgrade()方法,我們創(chuàng)建的表字段如下:
DBOpenHelper.java:
package com.jay.example.db;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DBOpenHelper extends SQLiteOpenHelper {
public DBOpenHelper(Context context) {
super(context, "downs.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
//數(shù)據(jù)庫的結(jié)構(gòu)為:表名:filedownlog 字段:id,downpath:當(dāng)前下載的資源,
//threadid:下載的線程id,downlength:線程下載的最后位置
db.execSQL("CREATE TABLE IF NOT EXISTS filedownlog " +
"(id integer primary key autoincrement," +
" downpath varchar(100)," +
" threadid INTEGER, downlength INTEGER)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//當(dāng)版本號發(fā)生改變時調(diào)用該方法,這里刪除數(shù)據(jù)表,在實際業(yè)務(wù)中一般是要進(jìn)行數(shù)據(jù)備份的
db.execSQL("DROP TABLE IF EXISTS filedownlog");
onCreate(db);
}
}
Step 2:創(chuàng)建一個數(shù)據(jù)庫操作類
我們需要創(chuàng)建什么樣的方法呢?
①我們需要一個根據(jù)URL獲得每條線程當(dāng)前下載長度的方法
②接著,當(dāng)我們的線程新開辟后,我們需要往數(shù)據(jù)庫中插入與該線程相關(guān)參數(shù)的方法
③還要定義一個可以實時更新下載文件長度的方法
④我們線程下載完,還需要根據(jù)線程id,刪除對應(yīng)記錄的方法
FileService.java
package com.jay.example.db;
import java.util.HashMap;
import java.util.Map;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
/*
* 該類是一個業(yè)務(wù)bean類,完成數(shù)據(jù)庫的相關(guān)操作
* */
public class FileService {
//聲明數(shù)據(jù)庫管理器
private DBOpenHelper openHelper;
//在構(gòu)造方法中根據(jù)上下文對象實例化數(shù)據(jù)庫管理器
public FileService(Context context) {
openHelper = new DBOpenHelper(context);
}
/**
* 獲得指定URI的每條線程已經(jīng)下載的文件長度
* @param path
* @return
* */
public Map<Integer, Integer> getData(String path)
{
//獲得可讀數(shù)據(jù)庫句柄,通常內(nèi)部實現(xiàn)返回的其實都是可寫的數(shù)據(jù)庫句柄
SQLiteDatabase db = openHelper.getReadableDatabase();
//根據(jù)下載的路徑查詢所有現(xiàn)場的下載數(shù)據(jù),返回的Cursor指向第一條記錄之前
Cursor cursor = db.rawQuery("select threadid, downlength from filedownlog where downpath=?",
new String[]{path});
//建立一個哈希表用于存放每條線程已下載的文件長度
Map<Integer,Integer> data = new HashMap<Integer, Integer>();
//從第一條記錄開始遍歷Cursor對象
cursor.moveToFirst();
while(cursor.moveToNext())
{
//把線程id與該線程已下載的長度存放到data哈希表中
data.put(cursor.getInt(0), cursor.getInt(1));
data.put(cursor.getInt(cursor.getColumnIndexOrThrow("threadid")),
cursor.getInt(cursor.getColumnIndexOrThrow("downlength")));
}
cursor.close();//關(guān)閉cursor,釋放資源;
db.close();
return data;
}
/**
* 保存每條線程已經(jīng)下載的文件長度
* @param path 下載的路徑
* @param map 現(xiàn)在的di和已經(jīng)下載的長度的集合
*/
public void save(String path,Map<Integer,Integer> map)
{
SQLiteDatabase db = openHelper.getWritableDatabase();
//開啟事務(wù),因為此處需要插入多條數(shù)據(jù)
db.beginTransaction();
try{
//使用增強(qiáng)for循環(huán)遍歷數(shù)據(jù)集合
for(Map.Entry<Integer, Integer> entry : map.entrySet())
{
//插入特定下載路徑特定線程ID已經(jīng)下載的數(shù)據(jù)
db.execSQL("insert into filedownlog(downpath, threadid, downlength) values(?,?,?)",
new Object[]{path, entry.getKey(), entry.getValue()});
}
//設(shè)置一個事務(wù)成功的標(biāo)志,如果成功就提交事務(wù),如果沒調(diào)用該方法的話那么事務(wù)回滾
//就是上面的數(shù)據(jù)庫操作撤銷
db.setTransactionSuccessful();
}finally{
//結(jié)束一個事務(wù)
db.endTransaction();
}
db.close();
}
/**
* 實時更新每條線程已經(jīng)下載的文件長度
* @param path
* @param map
*/
public void update(String path,int threadId,int pos)
{
SQLiteDatabase db = openHelper.getWritableDatabase();
//更新特定下載路徑下特定線程已下載的文件長度
db.execSQL("update filedownlog set downlength=? where downpath=? and threadid=?",
new Object[]{pos, path, threadId});
db.close();
}
/**
*當(dāng)文件下載完成后,刪除對應(yīng)的下載記錄
*@param path
*/
public void delete(String path)
{
SQLiteDatabase db = openHelper.getWritableDatabase();
db.execSQL("delete from filedownlog where downpath=?", new Object[]{path});
db.close();
}
}
Step 3:創(chuàng)建一個文件下載器類
好了,數(shù)據(jù)庫管理器與操作類都完成了接著就該弄一個文件下載器類了,在該類中又要完成 什么操作呢?要做的事就多了:
①定義一堆變量,核心是線程池threads和同步集合ConcurrentHashMap,用于緩存線程下載長度的
②定義一個獲取線程池中線程數(shù)的方法;
③定義一個退出下載的方法,
④獲取當(dāng)前文件大小的方法
⑤累計當(dāng)前已下載長度的方法,這里需要添加一個synchronized關(guān)鍵字,用來解決并發(fā)訪問的問題
⑥更新指定線程最后的下載位置,同樣也需要用同步
⑦在構(gòu)造方法中完成文件下載,線程開辟等操作
⑧獲取文件名的方法:先截取提供的url最后的'/'后面的字符串,如果獲取不到,再從頭字段查找,還是 找不到的話,就使用網(wǎng)卡標(biāo)識數(shù)字+cpu的唯一數(shù)字生成一個16個字節(jié)的二進(jìn)制作為文件名
⑨開始下載文件的方法
⑩獲取http響應(yīng)頭字段的方法
?打印http頭字段的方法
12.打印日志信息的方法
FileDownloadered.java:
package com.jay.example.service;
import java.io.File;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.content.Context;
import android.util.Log;
import com.jay.example.db.FileService;
public class FileDownloadered {
private static final String TAG = "文件下載類"; //設(shè)置一個查log時的一個標(biāo)志
private static final int RESPONSEOK = 200; //設(shè)置響應(yīng)碼為200,代表訪問成功
private FileService fileService; //獲取本地數(shù)據(jù)庫的業(yè)務(wù)Bean
private boolean exited; //停止下載的標(biāo)志
private Context context; //程序的上下文對象
private int downloadedSize = 0; //已下載的文件長度
private int fileSize = 0; //開始的文件長度
private DownloadThread[] threads; //根據(jù)線程數(shù)設(shè)置下載的線程池
private File saveFile; //數(shù)據(jù)保存到本地的文件中
private Map<Integer, Integer> data = new ConcurrentHashMap<Integer, Integer>(); //緩存?zhèn)€條線程的下載的長度
private int block; //每條線程下載的長度
private String downloadUrl; //下載的路徑
/**
* 獲取線程數(shù)
*/
public int getThreadSize()
{
//return threads.length;
return 0;
}
/**
* 退出下載
* */
public void exit()
{
this.exited = true; //將退出的標(biāo)志設(shè)置為true;
}
public boolean getExited()
{
return this.exited;
}
/**
* 獲取文件的大小
* */
public int getFileSize()
{
return fileSize;
}
/**
* 累計已下載的大小
* 使用同步鎖來解決并發(fā)的訪問問題
* */
protected synchronized void append(int size)
{
//把實時下載的長度加入到總的下載長度中
downloadedSize += size;
}
/**
* 更新指定線程最后下載的位置
* @param threadId 線程id
* @param pos 最后下載的位置
* */
protected synchronized void update(int threadId,int pos)
{
//把指定線程id的線程賦予最新的下載長度,以前的值會被覆蓋掉
this.data.put(threadId, pos);
//更新數(shù)據(jù)庫中制定線程的下載長度
this.fileService.update(this.downloadUrl, threadId, pos);
}
/**
* 構(gòu)建文件下載器
* @param downloadUrl 下載路徑
* @param fileSaveDir 文件的保存目錄
* @param threadNum 下載線程數(shù)
* @return
*/
public FileDownloadered(Context context,String downloadUrl,File fileSaveDir,int threadNum)
{
try {
this.context = context; //獲取上下文對象,賦值
this.downloadUrl = downloadUrl; //為下載路徑賦值
fileService = new FileService(this.context); //實例化數(shù)據(jù)庫操作的業(yè)務(wù)Bean類,需要傳一個context值
URL url = new URL(this.downloadUrl); //根據(jù)下載路徑實例化URL
if(!fileSaveDir.exists()) fileSaveDir.mkdir(); //如果文件不存在的話指定目錄,這里可創(chuàng)建多層目錄
this.threads = new DownloadThread[threadNum]; //根據(jù)下載的線程數(shù)量創(chuàng)建下載的線程池
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //創(chuàng)建遠(yuǎn)程連接句柄,這里并未真正連接
conn.setConnectTimeout(5000); //設(shè)置連接超時事件為5秒
conn.setRequestMethod("GET"); //設(shè)置請求方式為GET
//設(shè)置用戶端可以接收的媒體類型
conn.setRequestProperty("Accept", "image/gif, image/jpeg, image/pjpeg, " +
"image/pjpeg, application/x-shockwave-flash, application/xaml+xml, " +
"application/vnd.ms-xpsdocument, application/x-ms-xbap," +
" application/x-ms-application, application/vnd.ms-excel," +
" application/vnd.ms-powerpoint, application/msword, */*");
conn.setRequestProperty("Accept-Language", "zh-CN"); //設(shè)置用戶語言
conn.setRequestProperty("Referer", downloadUrl); //設(shè)置請求的來源頁面,便于服務(wù)端進(jìn)行來源統(tǒng)計
conn.setRequestProperty("Charset", "UTF-8"); //設(shè)置客戶端編碼
//設(shè)置用戶代理
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; " +
"Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727;" +
" .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");
conn.setRequestProperty("Connection", "Keep-Alive"); //設(shè)置connection的方式
conn.connect(); //和遠(yuǎn)程資源建立正在的鏈接,但尚無返回的數(shù)據(jù)流
printResponseHeader(conn); //打印返回的Http的頭字段集合
//對返回的狀態(tài)碼進(jìn)行判斷,用于檢查是否請求成功,返回200時執(zhí)行下面的代碼
if(conn.getResponseCode() == RESPONSEOK)
{
this.fileSize = conn.getContentLength(); //根據(jù)響應(yīng)獲得文件大小
if(this.fileSize <= 0)throw new RuntimeException("不知道文件大小"); //文件長度小于等于0時拋出運行時異常
String filename = getFileName(conn); //獲取文件名稱
this.saveFile = new File(fileSaveDir,filename); //根據(jù)文件保存目錄和文件名保存文件
Map<Integer,Integer> logdata = fileService.getData(downloadUrl); //獲取下載記錄
//如果存在下載記錄
if(logdata.size() > 0)
{
//遍歷集合中的數(shù)據(jù),把每條線程已下載的數(shù)據(jù)長度放入data中
for(Map.Entry<Integer, Integer> entry : logdata.entrySet())
{
data.put(entry.getKey(), entry.getValue());
}
}
//如果已下載的數(shù)據(jù)的線程數(shù)和現(xiàn)在設(shè)置的線程數(shù)相同時則計算所有現(xiàn)場已經(jīng)下載的數(shù)據(jù)總長度
if(this.data.size() == this.threads.length)
{
//遍歷每條線程已下載的數(shù)據(jù)
for(int i = 0;i < this.threads.length;i++)
{
this.downloadedSize += this.data.get(i+1);
}
print("已下載的長度" + this.downloadedSize + "個字節(jié)");
}
//使用條件運算符求出每個線程需要下載的數(shù)據(jù)長度
this.block = (this.fileSize % this.threads.length) == 0?
this.fileSize / this.threads.length:
this.fileSize / this.threads.length + 1;
}else{
//打印錯誤信息
print("服務(wù)器響應(yīng)錯誤:" + conn.getResponseCode() + conn.getResponseMessage());
throw new RuntimeException("服務(wù)器反饋出錯");
}
}catch (Exception e)
{
print(e.toString()); //打印錯誤
throw new RuntimeException("無法連接URL");
}
}
/**
* 獲取文件名
* */
private String getFileName(HttpURLConnection conn)
{
//從下載的路徑的字符串中獲取文件的名稱
String filename = this.downloadUrl.substring(this.downloadUrl.lastIndexOf('/') + 1);
if(filename == null || "".equals(filename.trim())){ //如果獲取不到文件名稱
for(int i = 0;;i++) //使用無限循環(huán)遍歷
{
String mine = conn.getHeaderField(i); //從返回的流中獲取特定索引的頭字段的值
if (mine == null) break; //如果遍歷到了返回頭末尾則退出循環(huán)
//獲取content-disposition返回字段,里面可能包含文件名
if("content-disposition".equals(conn.getHeaderFieldKey(i).toLowerCase())){
//使用正則表達(dá)式查詢文件名
Matcher m = Pattern.compile(".*filename=(.*)").matcher(mine.toLowerCase());
if(m.find()) return m.group(1); //如果有符合正則表達(dá)式規(guī)則的字符串,返回
}
}
filename = UUID.randomUUID()+ ".tmp";//如果都沒找到的話,默認(rèn)取一個文件名
//由網(wǎng)卡標(biāo)識數(shù)字(每個網(wǎng)卡都有唯一的標(biāo)識號)以及CPU時間的唯一數(shù)字生成的一個16字節(jié)的二進(jìn)制作為文件名
}
return filename;
}
/**
* 開始下載文件
* @param listener 監(jiān)聽下載數(shù)量的變化,如果不需要了解實時下載的數(shù)量,可以設(shè)置為null
* @return 已下載文件大小
* @throws Exception
*/
//進(jìn)行下載,如果有異常的話,拋出異常給調(diào)用者
public int download(DownloadProgressListener listener) throws Exception{
try {
RandomAccessFile randOut = new RandomAccessFile(this.saveFile, "rwd");
//設(shè)置文件大小
if(this.fileSize>0) randOut.setLength(this.fileSize);
randOut.close(); //關(guān)閉該文件,使設(shè)置生效
URL url = new URL(this.downloadUrl);
if(this.data.size() != this.threads.length){
//如果原先未曾下載或者原先的下載線程數(shù)與現(xiàn)在的線程數(shù)不一致
this.data.clear();
//遍歷線程池
for (int i = 0; i < this.threads.length; i++) {
this.data.put(i+1, 0);//初始化每條線程已經(jīng)下載的數(shù)據(jù)長度為0
}
this.downloadedSize = 0; //設(shè)置已經(jīng)下載的長度為0
}
for (int i = 0; i < this.threads.length; i++) {//開啟線程進(jìn)行下載
int downLength = this.data.get(i+1);
//通過特定的線程id獲取該線程已經(jīng)下載的數(shù)據(jù)長度
//判斷線程是否已經(jīng)完成下載,否則繼續(xù)下載
if(downLength < this.block && this.downloadedSize<this.fileSize){
//初始化特定id的線程
this.threads[i] = new DownloadThread(this, url, this.saveFile, this.block, this.data.get(i+1), i+1);
//設(shè)置線程優(yōu)先級,Thread.NORM_PRIORITY = 5;
//Thread.MIN_PRIORITY = 1;Thread.MAX_PRIORITY = 10,數(shù)值越大優(yōu)先級越高
this.threads[i].setPriority(7);
this.threads[i].start(); //啟動線程
}else{
this.threads[i] = null; //表明線程已完成下載任務(wù)
}
}
fileService.delete(this.downloadUrl);
//如果存在下載記錄,刪除它們,然后重新添加
fileService.save(this.downloadUrl, this.data);
//把下載的實時數(shù)據(jù)寫入數(shù)據(jù)庫中
boolean notFinish = true;
//下載未完成
while (notFinish) {
// 循環(huán)判斷所有線程是否完成下載
Thread.sleep(900);
notFinish = false;
//假定全部線程下載完成
for (int i = 0; i < this.threads.length; i++){
if (this.threads[i] != null && !this.threads[i].isFinish()) {
//如果發(fā)現(xiàn)線程未完成下載
notFinish = true;
//設(shè)置標(biāo)志為下載沒有完成
if(this.threads[i].getDownLength() == -1){
//如果下載失敗,再重新在已下載的數(shù)據(jù)長度的基礎(chǔ)上下載
//重新開辟下載線程,設(shè)置線程的優(yōu)先級
this.threads[i] = new DownloadThread(this, url, this.saveFile, this.block, this.data.get(i+1), i+1);
this.threads[i].setPriority(7);
this.threads[i].start();
}
}
}
if(listener!=null) listener.onDownloadSize(this.downloadedSize);
//通知目前已經(jīng)下載完成的數(shù)據(jù)長度
}
if(downloadedSize == this.fileSize) fileService.delete(this.downloadUrl);
//下載完成刪除記錄
} catch (Exception e) {
print(e.toString());
throw new Exception("文件下載異常");
}
return this.downloadedSize;
}
/**
* 獲取Http響應(yīng)頭字段
* @param http
* @return
*/
public static Map<String, String> getHttpResponseHeader(HttpURLConnection http) {
//使用LinkedHashMap保證寫入和便利的時候的順序相同,而且允許空值
Map<String, String> header = new LinkedHashMap<String, String>();
//此處使用無線循環(huán),因為不知道頭字段的數(shù)量
for (int i = 0;; i++) {
String mine = http.getHeaderField(i); //獲取第i個頭字段的值
if (mine == null) break; //沒值說明頭字段已經(jīng)循環(huán)完畢了,使用break跳出循環(huán)
header.put(http.getHeaderFieldKey(i), mine); //獲得第i個頭字段的鍵
}
return header;
}
/**
* 打印Http頭字段
* @param http
*/
public static void printResponseHeader(HttpURLConnection http){
//獲取http響應(yīng)的頭字段
Map<String, String> header = getHttpResponseHeader(http);
//使用增強(qiáng)for循環(huán)遍歷取得頭字段的值,此時遍歷的循環(huán)順序與輸入樹勛相同
for(Map.Entry<String, String> entry : header.entrySet()){
//當(dāng)有鍵的時候則獲取值,如果沒有則為空字符串
String key = entry.getKey()!=null ? entry.getKey()+ ":" : "";
print(key+ entry.getValue()); //打印鍵和值得組合
}
}
/**
* 打印信息
* @param msg 信息字符串
* */
private static void print(String msg) {
Log.i(TAG, msg);
}
}
Step 4:自定義一個下載線程類
這個自定義的線程類要做的事情如下:
① 首先肯定是要繼承Thread類啦,然后重寫Run()方法
② Run()方法:先判斷是否下載完成,沒有得話:打開URLConnection鏈接,接著RandomAccessFile 進(jìn)行數(shù)據(jù)讀寫,完成時設(shè)置完成標(biāo)記為true,發(fā)生異常的話設(shè)置長度為-1,打印異常信息
③打印log信息的方法
④判斷下載是否完成的方法(根據(jù)完成標(biāo)記)
⑤獲得已下載的內(nèi)容大小
DownLoadThread.java:
package com.jay.example.service;
import java.io.File;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import android.util.Log;
public class DownloadThread extends Thread {
private static final String TAG = "下載線程類"; //定義TAG,在打印log時進(jìn)行標(biāo)記
private File saveFile; //下載的數(shù)據(jù)保存到的文件
private URL downUrl; //下載的URL
private int block; //每條線程下載的大小
private int threadId = -1; //初始化線程id設(shè)置
private int downLength; //該線程已下載的數(shù)據(jù)長度
private boolean finish = false; //該線程是否完成下載的標(biāo)志
private FileDownloadered downloader; //文件下載器
public DownloadThread(FileDownloadered downloader, URL downUrl, File saveFile, int block, int downLength, int threadId) {
this.downUrl = downUrl;
this.saveFile = saveFile;
this.block = block;
this.downloader = downloader;
this.threadId = threadId;
this.downLength = downLength;
}
@Override
public void run() {
if(downLength < block){//未下載完成
try {
HttpURLConnection http = (HttpURLConnection) downUrl.openConnection();
http.setConnectTimeout(5 * 1000);
http.setRequestMethod("GET");
http.setRequestProperty("Accept", "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
http.setRequestProperty("Accept-Language", "zh-CN");
http.setRequestProperty("Referer", downUrl.toString());
http.setRequestProperty("Charset", "UTF-8");
int startPos = block * (threadId - 1) + downLength;//開始位置
int endPos = block * threadId -1;//結(jié)束位置
http.setRequestProperty("Range", "bytes=" + startPos + "-"+ endPos);//設(shè)置獲取實體數(shù)據(jù)的范圍
http.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");
http.setRequestProperty("Connection", "Keep-Alive");
InputStream inStream = http.getInputStream(); //獲得遠(yuǎn)程連接的輸入流
byte[] buffer = new byte[1024]; //設(shè)置本地數(shù)據(jù)的緩存大小為1MB
int offset = 0; //每次讀取的數(shù)據(jù)量
print("Thread " + this.threadId + " start download from position "+ startPos); //打印該線程開始下載的位置
RandomAccessFile threadfile = new RandomAccessFile(this.saveFile, "rwd");
threadfile.seek(startPos);
//用戶沒有要求停止下載,同時沒有達(dá)到請求數(shù)據(jù)的末尾時會一直循環(huán)讀取數(shù)據(jù)
while (!downloader.getExited() && (offset = inStream.read(buffer, 0, 1024)) != -1) {
threadfile.write(buffer, 0, offset); //直接把數(shù)據(jù)寫入到文件中
downLength += offset; //把新線程已經(jīng)寫到文件中的數(shù)據(jù)加入到下載長度中
downloader.update(this.threadId, downLength); //把該線程已經(jīng)下載的數(shù)據(jù)長度更新到數(shù)據(jù)庫和內(nèi)存哈希表中
downloader.append(offset); //把新下載的數(shù)據(jù)長度加入到已經(jīng)下載的數(shù)據(jù)總長度中
}
threadfile.close();
inStream.close();
print("Thread " + this.threadId + " download finish");
this.finish = true; //設(shè)置完成標(biāo)記為true,無論下載完成還是用戶主動中斷下載
} catch (Exception e) {
this.downLength = -1; //設(shè)置該線程已經(jīng)下載的長度為-1
print("Thread "+ this.threadId+ ":"+ e);
}
}
}
private static void print(String msg){
Log.i(TAG, msg);
}
/**
* 下載是否完成
* @return
*/
public boolean isFinish() {
return finish;
}
/**
* 已經(jīng)下載的內(nèi)容大小
* @return 如果返回值為-1,代表下載失敗
*/
public long getDownLength() {
return downLength;
}
}
Step 5:創(chuàng)建一個DownloadProgressListener接口監(jiān)聽下載進(jìn)度
FileDownloader中使用了DownloadProgressListener進(jìn)行進(jìn)度監(jiān)聽, 所以這里需要創(chuàng)建一個接口,同時定義一個方法的空實現(xiàn):
DownloadProgressListener.java:
package com.jay.example.service;
public interface DownloadProgressListener {
public void onDownloadSize(int downloadedSize);
}
Step 6:編寫我們的布局代碼
另外調(diào)用android:enabled="false"設(shè)置組件是否可點擊, 代碼如下
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.jay.example.multhreadcontinuabledemo.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="請輸入要下載的文件地址" />
<EditText
android:id="@+id/editpath"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="http://10.13.20.32:8080/Test/twelve.mp3"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btndown"
android:text="下載"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnstop"
android:text="停止"
android:enabled="false"
/>
<ProgressBar
android:layout_width="fill_parent"
android:layout_height="18dp"
style="?android:attr/progressBarStyleHorizontal"
android:id="@+id/progressBar"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:id="@+id/textresult"
android:text="顯示實時下載的百分比"
/>
</LinearLayout>
Step 7:MainActivity的編寫
最后就是我們的MainActivity了,完成組件以及相關(guān)變量的初始化; 使用handler來完成界面的更新操作,另外耗時操作不能夠在主線程中進(jìn)行, 所以這里需要開辟新的線程,這里用Runnable實現(xiàn),詳情見代碼 吧
MainActivity.java:
package com.jay.example.multhreadcontinuabledemo;
import java.io.File;
import com.jay.example.service.FileDownloadered;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText editpath;
private Button btndown;
private Button btnstop;
private TextView textresult;
private ProgressBar progressbar;
private static final int PROCESSING = 1; //正在下載實時數(shù)據(jù)傳輸Message標(biāo)志
private static final int FAILURE = -1; //下載失敗時的Message標(biāo)志
private Handler handler = new UIHander();
private final class UIHander extends Handler{
public void handleMessage(Message msg) {
switch (msg.what) {
//下載時
case PROCESSING:
int size = msg.getData().getInt("size"); //從消息中獲取已經(jīng)下載的數(shù)據(jù)長度
progressbar.setProgress(size); //設(shè)置進(jìn)度條的進(jìn)度
//計算已經(jīng)下載的百分比,此處需要轉(zhuǎn)換為浮點數(shù)計算
float num = (float)progressbar.getProgress() / (float)progressbar.getMax();
int result = (int)(num * 100); //把獲取的浮點數(shù)計算結(jié)果轉(zhuǎn)換為整數(shù)
textresult.setText(result+ "%"); //把下載的百分比顯示到界面控件上
if(progressbar.getProgress() == progressbar.getMax()){ //下載完成時提示
Toast.makeText(getApplicationContext(), "文件下載成功", 1).show();
}
break;
case FAILURE: //下載失敗時提示
Toast.makeText(getApplicationContext(), "文件下載失敗", 1).show();
break;
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editpath = (EditText) findViewById(R.id.editpath);
btndown = (Button) findViewById(R.id.btndown);
btnstop = (Button) findViewById(R.id.btnstop);
textresult = (TextView) findViewById(R.id.textresult);
progressbar = (ProgressBar) findViewById(R.id.progressBar);
ButtonClickListener listener = new ButtonClickListener();
btndown.setOnClickListener(listener);
btnstop.setOnClickListener(listener);
}
private final class ButtonClickListener implements View.OnClickListener{
public void onClick(View v) {
switch (v.getId()) {
case R.id.btndown:
String path = editpath.getText().toString();
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
File saveDir = Environment.getExternalStorageDirectory();
download(path, saveDir);
}else{
Toast.makeText(getApplicationContext(), "sd卡讀取失敗", 1).show();
}
btndown.setEnabled(false);
btnstop.setEnabled(true);
break;
case R.id.btnstop:
exit();
btndown.setEnabled(true);
btnstop.setEnabled(false);
break;
}
}
/*
由于用戶的輸入事件(點擊button, 觸摸屏幕....)是由主線程負(fù)責(zé)處理的,如果主線程處于工作狀態(tài),
此時用戶產(chǎn)生的輸入事件如果沒能在5秒內(nèi)得到處理,系統(tǒng)就會報“應(yīng)用無響應(yīng)”錯誤。
所以在主線程里不能執(zhí)行一件比較耗時的工作,否則會因主線程阻塞而無法處理用戶的輸入事件,
導(dǎo)致“應(yīng)用無響應(yīng)”錯誤的出現(xiàn)。耗時的工作應(yīng)該在子線程里執(zhí)行。
*/
private DownloadTask task;
/**
* 退出下載
*/
public void exit(){
if(task!=null) task.exit();
}
private void download(String path, File saveDir) {//運行在主線程
task = new DownloadTask(path, saveDir);
new Thread(task).start();
}
/*
* UI控件畫面的重繪(更新)是由主線程負(fù)責(zé)處理的,如果在子線程中更新UI控件的值,更新后的值不會重繪到屏幕上
* 一定要在主線程里更新UI控件的值,這樣才能在屏幕上顯示出來,不能在子線程中更新UI控件的值
*/
private final class DownloadTask implements Runnable{
private String path;
private File saveDir;
private FileDownloadered loader;
public DownloadTask(String path, File saveDir) {
this.path = path;
this.saveDir = saveDir;
}
/**
* 退出下載
*/
public void exit(){
if(loader!=null) loader.exit();
}
public void run() {
try {
loader = new FileDownloadered(getApplicationContext(), path, saveDir, 3);
progressbar.setMax(loader.getFileSize());//設(shè)置進(jìn)度條的最大刻度
loader.download(new com.jay.example.service.DownloadProgressListener() {
public void onDownloadSize(int size) {
Message msg = new Message();
msg.what = 1;
msg.getData().putInt("size", size);
handler.sendMessage(msg);
}
});
} catch (Exception e) {
e.printStackTrace();
handler.sendMessage(handler.obtainMessage(-1));
}
}
}
}
}
Step 8:AndroidManifest.xml文件中添加相關(guān)權(quán)限
訪問internet權(quán)限
<uses-permission android:name="android.permission.INTERNET"/>
在SDCard中創(chuàng)建與刪除文件權(quán)限
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
往SDCard寫入數(shù)據(jù)權(quán)限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
代碼下載:
多線程斷點下載器demo:MulThreadContinuableDemo.zip
多線程斷點下載+在線音樂播放器:多線程斷點下載+在線音樂播放器.zip