Android多執行緒+單執行緒+斷點續傳+進度條顯示下載

cmazxiaoma發表於2018-06-18

#####效果圖

download.gif

#####白話分析: 多執行緒:肯定是多個執行緒咯 斷點:執行緒停止下載的位置 續傳:執行緒從停止下載的位置上繼續下載,直到完成任務為止。

#####核心分析: ######斷點: 當前執行緒已經下載的資料長度

######續傳: 向伺服器請求上次執行緒停止下載位置的資料

con.setRequestProperty("Range", "bytes=" + start + "-" + end);
複製程式碼

######分配執行緒:

 int currentPartSize = fileSize / mThreadNum;
複製程式碼

######定義位置

定義執行緒開始下載的位置和結束的位置

for (int i = 0; i < mThreadNum; i++) {
int start = i * currentPartSize;//計算每條執行緒下載的開始位置
 int end = start + currentPartSize-1;//執行緒結束的位置
  if(i==mThreadNum-1){
      end=fileSize;
   }}
複製程式碼

######建立資料庫: 由於每一個檔案要分成多個部分,要被不同的執行緒同時進行下載。當然要建立執行緒表,儲存當前執行緒下載開始的位置和結束的位置,還有完成進度等。建立file表,儲存當前下載的檔案資訊,比如:檔名,url,下載進度等資訊 ######執行緒表:

public static final String CREATE_TABLE_SQL="create table "+TABLE_NAME+"(_id integer primary "
        +"key autoincrement, threadId, start , end, completed, url)";
複製程式碼

######file表:

public static final String CREATE_TABLE_SQL="create table "+TABLE_NAME+"(_id integer primary" +
        " key autoincrement ,fileName, url, length, finished)";
複製程式碼

######建立執行緒類 無非就2個類,一個是執行緒管理類DownLoadManager.java,核心方法:start(),stop(),restart(),addTask().clear()。另一個是執行緒任務類 DownLoadTask.java,就是一個執行緒類,用於下載執行緒分配好的任務。後面會貼出具體程式碼。

######建立資料庫方法類 無非就是單例模式,封裝一些增刪改查等基礎資料庫方法,後面會貼出具體程式碼。 ######建立實體類 也就是建立ThreadInfoFileInfo這2個實體類,把下載檔案資訊和執行緒資訊暫時儲存起來。 ######引入的第三方開源庫 NumberProgressBar是一個關於進度條的開源庫,挺不錯的。直達連結

#####程式碼具體分析 1.首先是建立實體類,檔案的實體類FileInfo,肯定有fileName,url,length,finised,isStop,isDownloading這些屬性。執行緒的實體類ThreadInfo肯定有threadId,start,end,completed,url這些屬性。這些都很簡單

//ThreadInfo.java
public class ThreadInfo {
private String threadId;
private int start;
private int end;
private int compeleted;
private String url;


public ThreadInfo(){

}

public ThreadInfo(String threadId, int start, int end, int compeleted, String url) {
    this.threadId = threadId;
    this.start = start;
    this.end = end;
    this.compeleted = compeleted;
    this.url = url;
}

public String getThreadId() {
    return threadId;
}

public void setThreadId(String threadId) {
    this.threadId = threadId;
}

public int getStart() {
    return start;
}

public void setStart(int start) {
    this.start = start;
}

public int getEnd() {
    return end;
}

public void setEnd(int end) {
    this.end = end;
}

public int getCompeleted() {
    return compeleted;
}

public void setCompeleted(int compeleted) {
    this.compeleted = compeleted;
}

public String getUrl() {
    return url;
}

public void setUrl(String url) {
    this.url = url;
}
複製程式碼

}

//FileInfo.java

public class FileInfo {
private String fileName; //檔名
private String url;  //下載地址
private int length;  //檔案大小
private int finished; //下載已完成進度
private boolean isStop=false; //是否暫停下載
private boolean isDownloading=false; //是否正在下載
public FileInfo(){

}

public FileInfo(String fileName,String url){
    this.fileName=fileName;
    this.url=url;
}

public String getFileName() {
    return fileName;
}

public void setFileName(String fileName) {
    this.fileName = fileName;
}

public String getUrl() {
    return url;
}

public void setUrl(String url) {
    this.url = url;
}

public int getLength() {
    return length;
}

public void setLength(int length) {
    this.length = length;
}

public int getFinished() {
    return finished;
}

public void setFinished(int finished) {
    this.finished = finished;
}

public boolean isStop() {
    return isStop;
}

public void setStop(boolean stop) {
    isStop = stop;
}

public boolean isDownloading() {
    return isDownloading;
}

public void setDownloading(boolean downloading) {
    isDownloading = downloading;
}

@Override
public String toString() {
    return "FileInfo{" +
            "fileName='" + fileName + '\'' +
            ", url='" + url + '\'' +
            ", length=" + length +
            ", finished=" + finished +
            ", isStop=" + isStop +
            ", isDownloading=" + isDownloading +
            '}';
}}
複製程式碼

2.實體類寫完了,那麼接下來寫建立一個類,繼承SQLiteOpenHelper類,來管理資料庫連線,主要作用:管理資料庫的初始化,並允許應用程式通過該類獲取SQLiteDatabase物件。

public class ThreadHelper extends SQLiteOpenHelper{
public static final String TABLE_NAME="downthread";
public static final String CREATE_TABLE_SQL="create table "+TABLE_NAME+"(_id integer primary "
        +"key autoincrement, threadId, start , end, completed, url)";


public ThreadHelper(Context context, String name, int version) {
    super(context, name, null, version);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_TABLE_SQL);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}}
複製程式碼

3.接下來封裝一些資料庫的增刪改查操作,用的單例模式,用雙重檢驗鎖實現單例。好處:既能很大程度上確保執行緒安全,又能實現延遲載入。 缺點:使用volatile關鍵字會使JVM對該程式碼的優化喪失,影響效能。並且在一些高併發的情況,仍然可能會建立多個例項,這稱為雙重檢驗鎖定失效。單例模式

public class Thread {
private SQLiteDatabase db;
public static final String DB_NAME="downthread.db3";
public static final int VERSION=1;
private  Context mContext;
private volatile static Thread t=null;

private Thread(){
    mContext= BaseApplication.getContext();
    db=new ThreadHelper(mContext,DB_NAME,VERSION).getReadableDatabase();
}

public static Thread getInstance(){
    if(t==null){
        synchronized (Thread.class){
            if(t==null){
                t=new Thread();
            }
        }
    }
    return t;
}

public SQLiteDatabase getDb(){
    return db;
}


//儲存當前執行緒下載進度
public synchronized void insert(ThreadInfo threadInfo){
    ContentValues values=new ContentValues();
    values.put("threadId",threadInfo.getThreadId());
    values.put("start",threadInfo.getStart());
    values.put("end",threadInfo.getEnd());
    values.put("completed",threadInfo.getCompeleted());
    values.put("url",threadInfo.getUrl());
    long rowId=db.insert(ThreadHelper.TABLE_NAME,null,values);
    if(rowId!=-1){
        UtilsLog.i("插入執行緒記錄成功");
    }else{
        UtilsLog.i("插入執行緒記錄失敗");
    }
}


//查詢當前執行緒 下載的進度
public synchronized ThreadInfo query(String threadId,String queryUrl){
    Cursor cursor=db.query(ThreadHelper.TABLE_NAME,null,"threadId= ? and url= ?",new String[]{threadId,queryUrl},null,null,null);
    ThreadInfo info=new ThreadInfo();
    if(cursor!=null){
        while (cursor.moveToNext()){
            int start=cursor.getInt(2);
            int end=cursor.getInt(3);
            int completed=cursor.getInt(4);
            String url=cursor.getString(5);
            info.setThreadId(threadId);
            info.setStart(start);
            info.setEnd(end);
            info.setCompeleted(completed);
            info.setUrl(url);
        }
        cursor.close();
    }
    return  info;
}


//更新當前執行緒下載進度
public synchronized void update(ThreadInfo info){
    ContentValues values=new ContentValues();
    values.put("start",info.getStart());
    values.put("completed",info.getCompeleted());
    db.update(ThreadHelper.TABLE_NAME,values,"threadId= ? and url= ?",new String[]{info.getThreadId(),info.getUrl()});
}

//關閉db
public void close(){
    db.close();
}

//判斷多執行緒任務下載 是否第一次建立執行緒
public boolean isExist(String url){
    Cursor cursor=db.query(ThreadHelper.TABLE_NAME,null,"url= ?",new String[]{url},null,null,null);
    boolean isExist=cursor.moveToNext();
    cursor.close();
    return isExist;
}

public synchronized void delete(ThreadInfo info){
    long rowId=db.delete(ThreadHelper.TABLE_NAME,"url =? and threadId= ?",new String[]{info.getUrl(),info.getThreadId()});
    if(rowId!=-1){
        UtilsLog.i("刪除下載執行緒記錄成功");
    }else{
        UtilsLog.i("刪除下載執行緒記錄失敗");
    }
}


public synchronized void delete(String url){
    long rowId=db.delete(ThreadHelper.TABLE_NAME,"url =? ",new String[]{url});
    if(rowId!=-1){
        UtilsLog.i("刪除下載執行緒記錄成功");
    }else{
        UtilsLog.i("刪除下載執行緒記錄失敗");
    }
}}
複製程式碼

4.基本的準備操作我們已經完成了,那麼開始寫關於下載的類吧。首先寫的肯定是DownLoadManager類,就是管理任務下載的類。不多說,直接看程式碼。

public class DownLoadManager {
private Map<String, FileInfo> map = new HashMap<>();
private static int mThreadNum;
private int fileSize;
private boolean flag = false; //true第一次下載 false不是第一次下載
private List<DownLoadTask> threads;
private static FileInfo mInfo;
private static ResultListener mlistener;
public static ExecutorService executorService = Executors.newCachedThreadPool();
public static File file;
private int totalComleted;

private DownLoadManager() {
    threads = new ArrayList<>();
}

public static DownLoadManager getInstance(FileInfo info, int threadNum,ResultListener listener) {
    mlistener = listener;
    mThreadNum = threadNum;
    mInfo = info;
    return DownLoadManagerHolder.dlm;
}

private static class DownLoadManagerHolder {
    private static final DownLoadManager dlm = new DownLoadManager();
}

public void start() {
    totalComleted=0;
    clear();
    final FileInfo newInfo = DownLoad.getInstance().queryData(mInfo.getUrl());
    newInfo.setDownloading(true);
    map.put(mInfo.getUrl(),newInfo);
    prepare(newInfo);
}

//停止下載任務
public void stop() {
    map.get(mInfo.getUrl()).setDownloading(false);
    map.get(mInfo.getUrl()).setStop(true);
}


public void clear(){
    if(threads.size()>0){
        threads.clear();
    }
}

//重新下載任務
public void restart() {
    stop();
    try {
        File file = new File(com.cmazxiaoma.downloader.download.DownLoadManager.FILE_PATH, map.get(mInfo.getUrl()).getFileName());
        if (file.exists()) {
            file.delete();
        }
        java.lang.Thread.sleep(100);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    DownLoad.getInstance().resetData(mInfo.getUrl());
    start();
}

//獲取當前任務狀態, 是否在下載
public boolean getCurrentState() {
    return map.get(mInfo.getUrl()).isDownloading();
}

//新增下載任務
public void addTask(FileInfo info) {
    //判斷資料庫是否已經存在此下載資訊
    if (!DownLoad.getInstance().isExist(info)) {
        DownLoad.getInstance().insertData(info);
        map.put(info.getUrl(), info);
    } else {
        DownLoad.getInstance().delete(info);
        DownLoad.getInstance().insertData(info);
        UtilsLog.i("map已經更新");
        map.remove(info.getUrl());
        map.put(info.getUrl(), info);
    }
}

private void prepare(final FileInfo newInfo) {
    new java.lang.Thread(){
        @Override
        public void run() {
            HttpURLConnection con = null;
            RandomAccessFile raf=null;
            try {
                //連線資源
                URL url = new URL(newInfo.getUrl());
                UtilsLog.i("url=" + url);
                con = (HttpURLConnection) url.openConnection();
                con.setConnectTimeout(2 * 1000);
                con.setRequestMethod("GET");
                int length = -1;
                UtilsLog.i("responseCode=" + con.getResponseCode());
                if (con.getResponseCode() == 200) {
                    length = con.getContentLength();
                    UtilsLog.i("檔案大小=" + length);
                }
                if (length <= 0) {
                    return;
                }
                //建立檔案儲存路徑
                File dir = new File(com.cmazxiaoma.downloader.download.DownLoadManager.FILE_PATH);
                if (!dir.exists()) {
                    dir.mkdirs();//建立多級資料夾
                }
                newInfo.setLength(length);
                fileSize = length;
                UtilsLog.i("當前執行緒Id=" + java.lang.Thread.currentThread().getId() + ",name=" + java.lang.Thread.currentThread().getName());
                int currentPartSize = fileSize / mThreadNum;
                file = new File(com.cmazxiaoma.downloader.download.DownLoadManager.FILE_PATH, newInfo.getFileName());
                raf = new RandomAccessFile(file, "rwd");
                raf.setLength(fileSize);
                if (Thread.getInstance().isExist(newInfo.getUrl())) {
                    flag = false;
                } else {
                    flag = true;
                }
                for (int i = 0; i < mThreadNum; i++) {
                    if (flag) {
                        UtilsLog.i("第一次多執行緒下載");
                        int start = i * currentPartSize;//計算每條執行緒下載的開始位置
                        int end = start + currentPartSize-1;//執行緒結束的位置
                        if(i==mThreadNum-1){
                            end=fileSize;
                        }
                        String threadId = "xiaoma" + i;
                        ThreadInfo threadInfo = new ThreadInfo(threadId, start, end, 0,newInfo.getUrl());
                        Thread.getInstance().insert(threadInfo);
                        DownLoadTask thread = new DownLoadTask(threadInfo,newInfo, threadId, start, end, 0);
                        DownLoadManager.executorService.execute(thread);
                        threads.add(thread);
                    } else {
                        UtilsLog.i("不是第一次多執行緒下載");
                        ThreadInfo threadInfo = Thread.getInstance().query("xiaoma" + i, newInfo.getUrl());
                        DownLoadTask thread = new DownLoadTask(threadInfo,newInfo,threadInfo.getThreadId(),threadInfo.getStart(),threadInfo.getEnd(),threadInfo.getCompeleted());//這裡出現過問題
                        DownLoadManager.executorService.execute(thread);
                        threads.add(thread);
                    }
                }

                boolean  isCompleted=false;
                while(!isCompleted){
                    isCompleted=true;
                    for(DownLoadTask thread:threads){
                        totalComleted+=thread.completed;
                        if(!thread.isCompleted){
                            isCompleted=false;
                        }
                    }

                    if(newInfo.isStop()){
                        totalComleted=0;
                        return;
                    }

                    Message message=new Message();
                    message.what=0x555;
                    message.arg1=fileSize;
                    message.arg2=totalComleted;
                    handler.sendMessage(message);

                    if(isCompleted){
                        totalComleted=0;
                        //任務執行緒全部完成,清空集合
                        clear();
                        handler.sendEmptyMessage(0x666);
                        return;
                    }

                    totalComleted=0;
                    java.lang.Thread.sleep(1000);
                }
            }catch (Exception e) {
                e.printStackTrace();
            }finally {
                try {
                    if (con != null) {
                        con.disconnect();
                    }
                    if(raf!=null){
                        raf.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }.start();
}


private Handler handler=new Handler(){
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        switch (msg.what){
            case 0x555:
                if(mlistener!=null){
                    mlistener.progress(msg.arg1,msg.arg2);
                }
                break;
            case 0x666:
                if(mlistener!=null){
                    mlistener.comleted();
                }
                break;
        }
    }
};}
複製程式碼

5.接下來呢,就是DownLoadTask類了,就是一個執行緒下載類。

public class DownLoadTask extends java.lang.Thread{
private int start;//當前執行緒的開始下載位置
private int end;//當前執行緒結束下載的位置
private RandomAccessFile raf;//當前執行緒負責下載的檔案大小
public int completed=0;//當前執行緒已下載的位元組數
private  String threadId;//自己定義的執行緒Id
private FileInfo info;
private ThreadInfo threadInfo;
public  boolean isCompleted=false; //true為當前執行緒完成任務,false為當前執行緒未完成任務
//儲存新的start
public int  finshed=0;
public int newStart=0;


public DownLoadTask(ThreadInfo threadInfo,FileInfo info,String threadId, int start, int end,int completed){
    this.threadInfo=threadInfo;
    this.info=info;
    this.threadId=threadId;
    this.start=start;
    this.end=end;
    this.completed=completed;
}


@Override
public void run() {
        HttpURLConnection con = null;
        try {
            UtilsLog.i("start="+start+",end="+end+",completed="+completed+",threadId="+getThreadId());
            URL url = new URL(info.getUrl());
            con = (HttpURLConnection) url.openConnection();
            con.setConnectTimeout(2 * 1000);
            con.setRequestMethod("GET");
            con.setRequestProperty("Range", "bytes=" + start + "-"+end);//重點
            raf=new RandomAccessFile(DownLoadManager.file,"rwd");
            //從檔案的某一位置寫入
            raf.seek(start);
            if (con.getResponseCode() == 206) { //檔案部分下載 返回碼是206
                InputStream is = con.getInputStream();
                byte[] buffer = new byte[4096];
                int hasRead = 0;
                while ((hasRead = is.read(buffer)) != -1) {
                    //寫入檔案
                    raf.write(buffer, 0, hasRead);
                    //單個檔案的完成程度
                    completed += hasRead;
                    threadInfo.setCompeleted(completed);
                    //儲存新的start
                    finshed=finshed+hasRead;//這裡出現過問題,嘻嘻
                    newStart=start+finshed;
                    threadInfo.setStart(newStart);
                   //UtilsLog.i("Thread:"+getThreadId()+",completed="     + completed);
                    //停止下載
                    if (info.isStop()) {
                        UtilsLog.i("isStop="+info.isStop());
                        //儲存下載進度
                        UtilsLog.i("現在Thread:"+getThreadId()+",completed=" + completed);
                        Thread.getInstance().update(threadInfo);
                        return;
                    }
                }
                //刪除該執行緒下載記錄
                Thread.getInstance().delete(threadInfo);
                isCompleted=true;
                Thread.getInstance().update(threadInfo);
                UtilsLog.i("thread:"+getThreadId()+"已經完成任務!--"+"completed="+completed);
            }

        } catch (Exception e) {
            if (con != null) {
                con.disconnect();
            }
            try {
                if (raf != null) {
                    raf.close();
                }

            } catch (IOException e1) {
                e1.printStackTrace();
            }

        }

    }

public String getThreadId() {
    return threadId;
}}
複製程式碼

6.介面,就是一個監聽下載進度的介面,也是很簡單。

public interface ResultListener{
void progress(int max, int progress);
void comleted();}
複製程式碼

######結束 大致操作就是這樣,其實多執行緒也挺簡單的。

相關文章