多任务多线程下载并不麻烦,只要思路清晰,逻辑清晰正确,是很好实现的。我最后遇到的纠结问题是数据库的操作上,我是拿数据库来存储下载信息的,所以在数据库的关闭上遇到了麻烦。上面那个版本是建立在前面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;
-
-
-
-
-
-
- public class DownloadDao
- {
- private DBHelper dbHelper;
-
- public DownloadDao(Context context)
- {
- dbHelper = new DBHelper(context);
- }
-
-
-
-
-
-
-
- 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;
- }
-
-
-
-
-
- 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);
- }
-
- }
-
-
-
-
-
-
- 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;
- }
-
-
-
-
-
-
-
- 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();
- }
-
-
-
-
-
-
- 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;
-
-
-
-
-
-
- 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)
- {
-
- }
-
- }