一、多線程斷點下載介紹
所謂的多線程斷點下載就是利用多線程下載,並且可被中斷,如果突然沒電了,重啟手機後可以繼續下載,而不需要重新下載;利用的技術有:SQLite存儲各個線程的下載量,HTTP請求獲得下載數據;
二、輔助類介紹
為了完成多線程斷點下載我們需要預先編寫一些輔助類:
(1)DBOpenHelper
(2)FileService:
-Map<Integer,Integer> getData(String path); 根據URL獲得各個線程的下載量
-save(String path, Map<Integer, Integer> map);存儲URL對應的各個線程下載量,此函數為剛剛開始時調用
-update(String path, Map<Integer, Integer> map);更新數據庫中URL對應的各個線程的下載量;
-delete(String path);刪除URL對應的數據;
(3)FileDownloader:
-getFileSize();獲得下載文件的大小
-download(DownloadProgressListener listener);下載文件,並設置監聽器
(4)DownloadThread:此類在FileDownloader的download中執行;
先將輔助類列出:
DBOpenHelper.java
-
- package service;
-
- import Android.content.Context;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
-
- public class DBOpenHelper extends SQLiteOpenHelper {
- private static final String DBNAME = "download.db";
- private static final int VERSION = 1;
-
- public DBOpenHelper(Context context) {
- super(context, DBNAME, null, VERSION);
- }
-
- @Override
- public void onCreate(SQLiteDatabase db) {
- 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) {
- db.execSQL("DROP TABLE IF EXISTS filedownlog");
- onCreate(db);
- }
-
- }
FileService.java
- package service;
-
- import java.util.HashMap;
- import java.util.Map;
-
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- /**
- * 業務bean
- *
- */
- public class FileService {
- private DBOpenHelper openHelper;
-
- public FileService(Context context) {
- openHelper = new DBOpenHelper(context);
- }
- /**
- * 獲取每條線程已經下載的文件長度
- * @param path
- * @return
- */
- public Map<Integer, Integer> getData(String path){
- SQLiteDatabase db = openHelper.getReadableDatabase();
- Cursor cursor = db.rawQuery("select threadid, downlength from filedownlog where downpath=?", new String[]{path});
- Map<Integer, Integer> data = new HashMap<Integer, Integer>();
- while(cursor.moveToNext()){
- data.put(cursor.getInt(0), cursor.getInt(1));
- }
- cursor.close();
- db.close();
- return data;
- }
- /**
- * 保存每條線程已經下載的文件長度
- * @param path
- * @param map
- */
- public void save(String path, Map<Integer, Integer> map){//int threadid, int position
- SQLiteDatabase db = openHelper.getWritableDatabase();
- db.beginTransaction();
- try{
- for(Map.Entry<Integer, Integer> entry : map.entrySet()){
- db.execSQL("insert into filedownlog(downpath, threadid, downlength) values(?,?,?)",
- new Object[]{path, entry.getKey(), entry.getValue()});
- }
- db.setTransactionSuccessful();
- }finally{
- db.endTransaction();
- }
- db.close();
- }
- /**
- * 實時更新每條線程已經下載的文件長度
- * @param path
- * @param map
- */
- public void update(String path, Map<Integer, Integer> map){
- SQLiteDatabase db = openHelper.getWritableDatabase();
- db.beginTransaction();
- try{
- for(Map.Entry<Integer, Integer> entry : map.entrySet()){
- db.execSQL("update filedownlog set downlength=? where downpath=? and threadid=?",
- new Object[]{entry.getValue(), path, entry.getKey()});
- }
- db.setTransactionSuccessful();
- }finally{
- db.endTransaction();
- }
- db.close();
- }
- /**
- * 當文件下載完成後,刪除對應的下載記錄
- * @param path
- */
- public void delete(String path){
- SQLiteDatabase db = openHelper.getWritableDatabase();
- db.execSQL("delete from filedownlog where downpath=?", new Object[]{path});
- db.close();
- }
-
- }