Android实现多任务多线程支持断点续传的下载软件
多任务多线程下载并不麻烦,只要思路清晰,逻辑清晰正确,是很好实现的。我最后遇到的纠结问题是数据库的操作上,我是拿数据库来存储下载信息的,所以在数据库的关闭上遇到了麻烦。上面那个版本是建立在前面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)
- {
- }
- }
相关推荐
缘来如此 2019-12-04
whale 2019-06-26
ameng 2010-09-25
starzhangkiss 2020-07-18
IT影风 2020-01-19
vanturman 2019-12-13
scratlc 2019-07-11
liqing00 2018-08-23
lemonzhangyan 2019-09-02
zlfing 2013-12-18
88590191 2010-02-09
84487600 2019-06-28
SoccerZZM 2011-08-16
ROES 2016-05-07
前端学习笔记 2019-05-08
张小程 2016-10-17
zhangbigang00 2016-10-14