多任務多線程下載並不麻煩,只要思路清晰,邏輯清晰正確,是很好實現的。我最後遇到的糾結問題是數據庫的操作上,我是拿數據庫來存儲下載信息的,所以在數據庫的關閉上遇到了麻煩。上面那個版本是建立在前面N個demo的基礎之上的,在這裡我寫下來的唯一目的就是能夠以一個清晰的思路寫清楚,同時讓大家看明白。
運行效果圖:
一、首先是數據庫,
數據庫五個字段:
任務的ID:_id
線程ID:thread_id
線程下載的起始位置:start_pos
這個線程下載的結束位置:end_pos
這個任務已經下載的大小:compelete_size
這個任務的下載地址:urlString
- create table download_info(_id integer PRIMARY KEY AUTOINCREMENT, thread_id integer,start_pos integer, end_pos integer, compelete_size integer,urlString char)
二、操作數據庫的類。在實現斷點續傳下載的時候,我是把線程每次下載結束後的當前任務信息都保存到數據庫裡面一次,相當於每次一個線程下載一次,就給當前任務拍個照片,把當前信息存到數據庫裡面。這樣一旦暫停,或者退出程序,下次再下載的時候,直接從數據庫裡面讀數據,然後在這個數據的基礎上繼續下載就行。
- package com.song.dao;
-
- import java.util.ArrayList;
- import java.util.List;
-
- import Android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
-
- import com.song.db.DBHelper;
- import com.song.entity.ThreadDownloadInfo;
-
- /**
- * 操作數據庫
- * @author song
- *
- */
- public class DownloadDao
- {
- private DBHelper dbHelper;
-
- public DownloadDao(Context context)
- {
- dbHelper = new DBHelper(context);
- }
-
- /**
- *
- * 判斷數據庫中是不是有對應這個urlString的信息
- *
- * @return
- */
- public boolean unhasInfo(String urlString)
- {
- SQLiteDatabase db = dbHelper.getReadableDatabase();
- String sql = "select count(*) from download_info where urlString=?";
- Cursor cursor = db.rawQuery(sql, new String[]{urlString});
- cursor.moveToFirst();
- int count = cursor.getInt(0);
- cursor.close();
- return count == 0;
- }
-
- /**
- * 把線程信息保存在數據庫裡面
- * @param infos
- */
- public void saveInfos(List<ThreadDownloadInfo> infos)
- {
- SQLiteDatabase db = dbHelper.getWritableDatabase();
- for (ThreadDownloadInfo info : infos)
- {
- String sql = "insert into download_info(thread_id,start_pos, end_pos,compelete_size,urlString) values (?,?,?,?,?)";
- Object[] bindArgs =
- { info.getThreadId(), info.getStartPos(), info.getEndPos(),
- info.getCompleteSize() ,info.getUrlString()};
- db.execSQL(sql, bindArgs);
- }
-
- }
-
- /**
- * 暫停之後,把當前數據保存在數據庫中,該方法是從數據庫中查詢數據
- *
- * @return
- */
- public List<ThreadDownloadInfo> getInfos(String urlString)
- {
- List<ThreadDownloadInfo> list = new ArrayList<ThreadDownloadInfo>();
- SQLiteDatabase db = dbHelper.getReadableDatabase();
- String sql = "select thread_id, start_pos, end_pos,compelete_size, urlString from download_info where urlString=?";
- Cursor cursor = db.rawQuery(sql, new String[]{urlString});
- while (cursor.moveToNext())
- {
- ThreadDownloadInfo info = new ThreadDownloadInfo(cursor.getInt(0),
- cursor.getInt(1), cursor.getInt(2), cursor.getInt(3),cursor.getString(4));
- list.add(info);
- }
- cursor.close();
- return list;
- }
-
- /**
- * 把當前的數據照片 存進數據庫中
- *
- * @param threadId
- * @param completeSize
- */
- public void updateInfo(int threadId, int completeSize,String urlString)
- {
- SQLiteDatabase db = dbHelper.getWritableDatabase();
- String sql = "update download_info set compelete_size=? where thread_id=? and urlString=?";
- Object[] bindArgs =
- { completeSize, threadId,urlString };
- db.execSQL(sql, bindArgs);
- }
-
- /**
- * 關閉數據庫
- */
- public void closeDB()
- {
- dbHelper.close();
- }
-
- /**
- * 下載完成之後,從數據庫裡面把這個任務的信息刪除
- * 不同的任務對應不同的urlString
- * @param urlString
- */
- public void deleteInfos(String urlString)
- {
- SQLiteDatabase db=dbHelper.getWritableDatabase();
- db.delete("download_info", "urlString=?", new String[]{urlString});
- }
- }
數據庫助手類DBhelper:
- package com.song.db;
-
- import android.content.Context;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import android.util.Log;
-
- /**
- * 數據庫助手類
- * @author song
- *
- */
- public class DBHelper extends SQLiteOpenHelper
- {
-
- public DBHelper(Context context)
- {
- super(context, "download.db", null, 1);
- }
-
- @Override
- public void onCreate(SQLiteDatabase db)
- {
- Log.v("TAG", "DBHelper-->conCreate()");
- String sql = "create table download_info(_id integer PRIMARY KEY AUTOINCREMENT, thread_id integer,start_pos integer, end_pos integer, compelete_size integer,urlString char)";
- db.execSQL(sql);
- }
-
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
- {
-
- }
-
- }