Android之断点续传下载 [复制链接]

 

今天学习了Android开发中比较难的一个环节,就是断点续传下载,很多人看到这个标题就感觉头大,的确,如果没有良好的逻辑思维,这块的确很难搞明白。下面我就将自己学到的知识和一些见解写下供那些在这个环节还烦恼的人参考。这里我以下载mp3文件为例。

断点续传下载,顾名思义,那就是我们在一次下载未结束时,退出下载,第二次下载时会接着第一次下载的进度继续下载。那么怎么记录第一次下载的数据呢,这里肯定就要用到数据库了。下面就是我创建数据库的一个SQLiteOpenHelper类。用来首次运行时创建数据库。

DBHelper类
 01.1 package cn.yj3g.DBHelper;
  
 02. 2 03. 3 import android.content.Context;
  
 04. 4 import android.database.sqlite.SQLiteDatabase;
  
 05. 5 import android.database.sqlite.SQLiteOpenHelper;
  
 06. 6 07. 7     /**
  
 08. 8      * 建立一个数据库帮助类
  
 09. 9      */
  
 10.10 public class DBHelper extends SQLiteOpenHelper {
  
 11.11     //download.db-->数据库名
  
 12.12     public DBHelper(Context context) {
  
 13.13         super(context, "download.db", null, 1);
  
 14.14     }
  
 15.15     
  
 16.16     /**
  
 17.17      * 在download.db数据库下创建一个download_info表存储下载信息
  
 18.18      */
  
 19.19     @Override
  
 20.20     public void onCreate(SQLiteDatabase db) {
  
 21.21         db.execSQL("create table download_info(_id integer PRIMARY KEY AUTOINCREMENT, thread_id integer, "
  
 22.22                 + "start_pos integer, end_pos integer, compelete_size integer,url char)");
  
 23.23     }
  
 24.24     @Override
  
 25.25     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  
 26.26 27.27     }
  
 28.28 29.29 }
 复制代码
 下面来看主界面的布局,在这里,我只设计了一个ListView来显示下载的音乐的名称,和一个开始下载按钮和一个暂停按钮。布局文件如下:
main.xml:
 01.1 <?xml version="1.0" encoding="utf-8"?>
  
 02. 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  
 03. 3     android:orientation="vertical"
  
 04. 4     android:layout_width="fill_parent"
  
 05. 5     android:layout_height="fill_parent"
  
 06. 6     android:id="@+id/llRoot">
  
 07. 7     <ListView android:id="@android:id/list"
  
 08. 8         android:layout_width="fill_parent"
  
 09. 9         android:layout_height="fill_parent">
  
 10.10     </ListView>
  
 11.11 </LinearLayout>
 复制代码list_item.xml:01.1 <?xml version="1.0" encoding="utf-8"?>
  
 02. 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  
 03. 3            android:orientation="vertical"
  
 04. 4            android:layout_width="fill_parent"
  
 05. 5            android:layout_height="wrap_content">
  
 06. 6     <LinearLayout
  
 07. 7            android:orientation="horizontal"
  
 08. 8            android:layout_width="fill_parent"
  
 09. 9            android:layout_height="wrap_content"
  
 10.10            android:layout_marginBottom="5dip">
  
 11.11         <TextView 12.12             android:layout_width="fill_parent"
  
 13.13             android:layout_height="wrap_content"
  
 14.14             android:layout_weight="1"
  
 15.15             android:id="@+id/tv_resouce_name"/>
  
 16.16         <Button
  
 17.17             android:layout_width="fill_parent"
  
 18.18             android:layout_height="wrap_content"
  
 19.19             android:layout_weight="1"
  
 20.20             android:text="下载"
  
 21.21             android:id="@+id/btn_start"
  
 22.22             android:onClick="startDownload"/>
  
 23.23         <Button
  
 24.24             android:layout_width="fill_parent"
  
 25.25             android:layout_height="wrap_content"
  
 26.26             android:layout_weight="1"
  
 27.27             android:text="暂停"
  
 28.28             android:id="@+id/btn_pause"
  
 29.29             android:onClick="pauseDownload"/>
  
 30.30       </LinearLayout>
  
 31.31 </LinearLayout>
 复制代码
 主界面运行效果如下: 
 
 
 
下面我们来看具体实现下载的方法。首先,我们要定义一个记录在下载时各个时期的数据的类,这里我创建了一个DownloadInfo类来记录。代码如下:
DownloadInfo:
 01. 1 package cn.yj3g.entity;
  
 02. 2 /**
  
 03. 3  *创建一个下载信息的实体类
  
 04. 4  */
  
 05. 5 public class DownloadInfo {
  
 06. 6     private int threadId;//下载器id
  
 07. 7     private int startPos;//开始点
  
 08. 8     private int endPos;//结束点
  
 09. 9     private int compeleteSize;//完成度
  
 10.10     private String url;//下载器网络标识
  
 11.11     public DownloadInfo(int threadId, int startPos, int endPos,
  
 12.12             int compeleteSize,String url) {
  
 13.13         this.threadId = threadId;
  
 14.14         this.startPos = startPos;
  
 15.15         this.endPos = endPos;
  
 16.16         this.compeleteSize = compeleteSize;
  
 17.17         this.url=url;
  
 18.18     }
  
 19.19     public DownloadInfo() {
  
 20.20     }
  
 21.21     public String getUrl() {
  
 22.22         return url;
  
 23.23     }
  
 24.24     public void setUrl(String url) {
  
 25.25         this.url = url;
  
 26.26     }
  
 27.27     public int getThreadId() {
  
 28.28         return threadId;
  
 29.29     }
  
 30.30     public void setThreadId(int threadId) {
  
 31.31         this.threadId = threadId;
  
 32.32     }
  
 33.33     public int getStartPos() {
  
 34.34         return startPos;
  
 35.35     }
  
 36.36     public void setStartPos(int startPos) {
  
 37.37         this.startPos = startPos;
  
 38.38     }
  
 39.39     public int getEndPos() {
  
 40.40         return endPos;
  
 41.41     }
  
 42.42     public void setEndPos(int endPos) {
  
 43.43         this.endPos = endPos;
  
 44.44     }
  
 45.45     public int getCompeleteSize() {
  
 46.46         return compeleteSize;
  
 47.47     }
  
 48.48     public void setCompeleteSize(int compeleteSize) {
  
 49.49         this.compeleteSize = compeleteSize;
  
 50.50     }
  
 51.51 52.52     @Override
  
 53.53     public String toString() {
  
 54.54         return "DownloadInfo [threadId=" + threadId
  
 55.55                 + ", startPos=" + startPos + ", endPos=" + endPos
  
 56.56                 + ", compeleteSize=" + compeleteSize +"]";
  
 57.57     }
  
 58.58 }
 复制代码
 在下载时,我们有进度条来显示进度,怎么确定进度条的进度,大小和起始位置呢?这里我定义了一个LoadInfo类来记录下载器详细信息。代码如下:LoadInfo:
 01. 1 package cn.yj3g.entity;
  
 02. 2 /**
  
 03. 3  *自定义的一个记载下载器详细信息的类 04. 4  */
  
 05. 5 public class LoadInfo {
  
 06. 6     public int fileSize;//文件大小
  
 07. 7     private int complete;//完成度
  
 08. 8     private String urlstring;//下载器标识
  
 09. 9     public LoadInfo(int fileSize, int complete, String urlstring) {
  
 10.10         this.fileSize = fileSize;
  
 11.11         this.complete = complete;
  
 12.12         this.urlstring = urlstring;
  
 13.13     }
  
 14.14     public LoadInfo() {
  
 15.15     }
  
 16.16     public int getFileSize() {
  
 17.17         return fileSize;
  
 18.18     }
  
 19.19     public void setFileSize(int fileSize) {
  
 20.20         this.fileSize = fileSize;
  
 21.21     }
  
 22.22     public int getComplete() {
  
 23.23         return complete;
  
 24.24     }
  
 25.25     public void setComplete(int complete) {
  
 26.26         this.complete = complete;
  
 27.27     }
  
 28.28     public String getUrlstring() {
  
 29.29         return urlstring;
  
 30.30     }
  
 31.31     public void setUrlstring(String urlstring) {
  
 32.32         this.urlstring = urlstring;
  
 33.33     }
  
 34.34     @Override
  
 35.35     public String toString() {
  
 36.36         return "LoadInfo [fileSize=" + fileSize + ", complete=" + complete
  
 37.37                 + ", urlstring=" + urlstring + "]";
  
 38.38     }
  
 39.39 }
 复制代码
 下面是最最重要的一步,那就是定义一个下载器来进行下载了,这里我就不多说,具体解释在代码中都有注解,我就直接将代码附下,供大家研究参考Downloader:
 01. 1 package cn.yj3g.service;
  
 02.  2 03.  3 import java.io.File;
  
 04.  4 import java.io.InputStream;
  
 05.  5 import java.io.RandomAccessFile;
  
 06.  6 import java.net.HttpURLConnection;
  
 07.  7 import java.net.URL;
  
 08.  8 import java.util.ArrayList;
  
 09.  9 import java.util.List;
  
 10. 10 import android.content.Context;
  
 11. 11 import android.os.Handler;
  
 12. 12 import android.os.Message;
  
 13. 13 import android.util.Log;
  
 14. 14 import cn.yj3g.Dao.Dao;
  
 15. 15 import cn.yj3g.entity.DownloadInfo;
  
 16. 16 import cn.yj3g.entity.LoadInfo;
  
 17. 17 18. 18 public class Downloader {
  
 19. 19     private String urlstr;// 下载的地址
  
 20. 20     private String localfile;// 保存路径
  
 21. 21     private int threadcount;// 线程数
  
 22. 22     private Handler mHandler;// 消息处理器
  
 23. 23     private Dao dao;// 工具类
  
 24. 24     private int fileSize;// 所要下载的文件的大小
  
 25. 25     private List<DownloadInfo> infos;// 存放下载信息类的集合
  
 26. 26     private static final int INIT = 1;//定义三种下载的状态:初始化状态,正在下载状态,暂停状态
  
 27. 27     private static final int DOWNLOADING = 2;
  
 28. 28     private static final int PAUSE = 3;
  
 29. 29     private int state = INIT;
  
 30. 30 31. 31     public Downloader(String urlstr, String localfile, int threadcount,
  
 32. 32             Context context, Handler mHandler) {
  
 33. 33         this.urlstr = urlstr;
  
 34. 34         this.localfile = localfile;
  
 35. 35         this.threadcount = threadcount;
  
 36. 36         this.mHandler = mHandler;
  
 37. 37         dao = new Dao(context);
  
 38. 38     }
  
 39. 39     /**
  
 40. 40      *判断是否正在下载 41. 41      */
  
 42. 42     public boolean isdownloading() {
  
 43. 43         return state == DOWNLOADING;
  
 44. 44     }
  
 45. 45     /**
  
 46. 46      * 得到downloader里的信息
  
 47. 47      * 首先进行判断是否是第一次下载,如果是第一次就要进行初始化,并将下载器的信息保存到数据库中
  
 48. 48      * 如果不是第一次下载,那就要从数据库中读出之前下载的信息(起始位置,结束为止,文件大小等),并将下载信息返回给下载器
  
 49. 49      */
  
 50. 50     public LoadInfo getDownloaderInfors() {
  
 51. 51         if (isFirst(urlstr)) {
  
 52. 52             Log.v("TAG", "isFirst");
  
 53. 53             init();
  
 54. 54             int range = fileSize / threadcount;
  
 55. 55             infos = new ArrayList<DownloadInfo>();
  
 56. 56             for (int i = 0; i < threadcount - 1; i++) {
  
 57. 57                 DownloadInfo info = new DownloadInfo(i, i * range, (i + 1)* range - 1, 0, urlstr);
  
 58. 58                 infos.add(info);
  
 59. 59             }
  
 60. 60             DownloadInfo info = new DownloadInfo(threadcount - 1,(threadcount - 1) * range, fileSize - 1, 0, urlstr);
  
 61. 61             infos.add(info);
  
 62. 62             //保存infos中的数据到数据库
  
 63. 63             dao.saveInfos(infos);
  
 64. 64             //创建一个LoadInfo对象记载下载器的具体信息
  
 65. 65             LoadInfo loadInfo = new LoadInfo(fileSize, 0, urlstr);
  
 66. 66             return loadInfo;
  
 67. 67         } else {
  
 68. 68             //得到数据库中已有的urlstr的下载器的具体信息
  
 69. 69             infos = dao.getInfos(urlstr);
  
 70. 70             Log.v("TAG", "not isFirst size=" + infos.size());
  
 71. 71             int size = 0;
  
 72. 72             int compeleteSize = 0;
  
 73. 73             for (DownloadInfo info : infos) {
  
 74. 74                 compeleteSize += info.getCompeleteSize();
  
 75. 75                 size += info.getEndPos() - info.getStartPos() + 1;
  
 76. 76             }
  
 77. 77             return new LoadInfo(size, compeleteSize, urlstr);
  
 78. 78         }
  
 79. 79     }
  
 80. 80 81. 81     /**
  
 82. 82      * 初始化
  
 83. 83      */
  
 84. 84     private void init() {
  
 85. 85         try {
  
 86. 86             URL url = new URL(urlstr);
  
 87. 87             HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  
 88. 88             connection.setConnectTimeout(5000);
  
 89. 89             connection.setRequestMethod("GET");
  
 90. 90             fileSize = connection.getContentLength();
  
 91. 91 92. 92             File file = new File(localfile);
  
 93. 93             if (!file.exists()) {
  
 94. 94                 file.createNewFile();
  
 95. 95             }
  
 96. 96             // 本地访问文件
  
 97. 97             RandomAccessFile accessFile = new RandomAccessFile(file, "rwd");
  
 98. 98             accessFile.setLength(fileSize);
  
 99. 99             accessFile.close();
  
 100.100             connection.disconnect();
  
 101.101         } catch (Exception e) {
  
 102.102             e.printStackTrace();
  
 103.103         }
  
 104.104     }
  
 105.105 106.106     /**
  
 107.107      * 判断是否是第一次 下载
  
 108.108      */
  
 109.109     private boolean isFirst(String urlstr) {
  
 110.110         return dao.isHasInfors(urlstr);
  
 111.111     }
  
 112.112 113.113     /**
  
 114.114      * 利用线程开始下载数据
  
 115.115      */
  
 116.116     public void download() {
  
 117.117         if (infos != null) {
  
 118.118             if (state == DOWNLOADING)
  
 119.119                 return;
  
 120.120             state = DOWNLOADING;
  
 121.121             for (DownloadInfo info : infos) {
  
 122.122                 new MyThread(info.getThreadId(), info.getStartPos(),
  
 123.123                         info.getEndPos(), info.getCompeleteSize(),
  
 124.124                         info.getUrl()).start();
  
 125.125             }
  
 126.126         }
  
 127.127     }
  
 128.128 129.129     public class MyThread extends Thread {
  
 130.130         private int threadId;
  
 131.131         private int startPos;
  
 132.132         private int endPos;
  
 133.133         private int compeleteSize;
  
 134.134         private String urlstr;
  
 135.135 136.136         public MyThread(int threadId, int startPos, int endPos,
  
 137.137                 int compeleteSize, String urlstr) {
  
 138.138             this.threadId = threadId;
  
 139.139             this.startPos = startPos;
  
 140.140             this.endPos = endPos;
  
 141.141             this.compeleteSize = compeleteSize;
  
 142.142             this.urlstr = urlstr;
  
 143.143         }
  
 144.144         @Override
  
 145.145         public void run() {
  
 146.146             HttpURLConnection connection = null;
  
 147.147             RandomAccessFile randomAccessFile = null;
  
 148.148             InputStream is = null;
  
 149.149             try {
  
 150.150                 URL url = new URL(urlstr);
  
 151.151                 connection = (HttpURLConnection) url.openConnection();
  
 152.152                 connection.setConnectTimeout(5000);
  
 153.153                 connection.setRequestMethod("GET");
  
 154.154                 // 设置范围,格式为Range:bytes x-y;
  
 155.155                 connection.setRequestProperty("Range", "bytes="+(startPos + compeleteSize) + "-" + endPos);
  
 156.156 157.157                 randomAccessFile = new RandomAccessFile(localfile, "rwd");
  
 158.158                 randomAccessFile.seek(startPos + compeleteSize);
  
 159.159                 // 将要下载的文件写到保存在保存路径下的文件中
  
 160.160                 is = connection.getInputStream();
  
 161.161                 byte[] buffer = new byte[4096];
  
 162.162                 int length = -1;
  
 163.163                 while ((length = is.read(buffer)) != -1) {
  
 164.164                     randomAccessFile.write(buffer, 0, length);
  
 165.165                     compeleteSize += length;
  
 166.166                     // 更新数据库中的下载信息
  
 167.167                     dao.updataInfos(threadId, compeleteSize, urlstr);
  
 168.168                     // 用消息将下载信息传给进度条,对进度条进行更新
  
 169.169                     Message message = Message.obtain();
  
 170.170                     message.what = 1;
  
 171.171                     message.obj = urlstr;
  
 172.172                     message.arg1 = length;
  
 173.173                     mHandler.sendMessage(message);
  
 174.174                     if (state == PAUSE) {
  
 175.175                         return;
  
 176.176                     }
  
 177.177                 }
  
 178.178             } catch (Exception e) {
  
 179.179                 e.printStackTrace();
  
 180.180             } finally {
  
 181.181                 try {
  
 182.182                     is.close();
  
 183.183                     randomAccessFile.close();
  
 184.184                     connection.disconnect();
  
 185.185                     dao.closeDb();
  
 186.186                 } catch (Exception e) {
  
 187.187                     e.printStackTrace();
  
 188.188                 }
  
 189.189             }
  
 190.190 191.191         }
  
 192.192     }
  
 193.193     //删除数据库中urlstr对应的下载器信息
  
 194.194     public void delete(String urlstr) {
  
 195.195         dao.delete(urlstr);
  
 196.196     }
  
 197.197     //设置暂停
  
 198.198     public void pause() {
  
 199.199         state = PAUSE;
  
 200.200     }
  
 201.201     //重置下载状态
  
 202.202     public void reset() {
  
 203.203         state = INIT;
  
 204.204     }
  
 205.205 }
 复制代码
 在这边下载器类的定义中,我们用到了许多关于进行数据库操作的方法,这里我定义了一个数据库工具类,来提供这些方法,代码如下:Dao:
 01. 1 package cn.yj3g.Dao;
  
 02. 2 03. 3 import java.util.ArrayList;
  
 04. 4 import java.util.List;
  
 05. 5 import android.content.Context;
  
 06. 6 import android.database.Cursor;
  
 07. 7 import android.database.sqlite.SQLiteDatabase;
  
 08. 8 import cn.yj3g.DBHelper.DBHelper;
  
 09. 9 import cn.yj3g.entity.DownloadInfo;
  
 10.10 11.11 /**
  
 12.12  * 13.13  * 一个业务类
  
 14.14  */
  
 15.15 public class Dao {
  
 16.16     private DBHelper dbHelper;
  
 17.17 18.18     public Dao(Context context) {
  
 19.19         dbHelper = new DBHelper(context);
  
 20.20     }
  
 21.21 22.22     /**
  
 23.23      * 查看数据库中是否有数据
  
 24.24      */
  
 25.25     public boolean isHasInfors(String urlstr) {
  
 26.26         SQLiteDatabase database = dbHelper.getReadableDatabase();
  
 27.27         String sql = "select count(*)  from download_info where url=?";
  
 28.28         Cursor cursor = database.rawQuery(sql, new String[] { urlstr });
  
 29.29         cursor.moveToFirst();
  
 30.30         int count = cursor.getInt(0);
  
 31.31         cursor.close();
  
 32.32         return count == 0;
  
 33.33     }
  
 34.34 35.35     /**
  
 36.36      * 保存 下载的具体信息
  
 37.37      */
  
 38.38     public void saveInfos(List<DownloadInfo> infos) {
  
 39.39         SQLiteDatabase database = dbHelper.getWritableDatabase();
  
 40.40         for (DownloadInfo info : infos) {
  
 41.41             String sql = "insert into download_info(thread_id,start_pos, end_pos,compelete_size,url) values (?,?,?,?,?)";
  
 42.42             Object[] bindArgs = { info.getThreadId(), info.getStartPos(),
  
 43.43                     info.getEndPos(), info.getCompeleteSize(), info.getUrl() };
  
 44.44             database.execSQL(sql, bindArgs);
  
 45.45         }
  
 46.46     }
  
 47.47 48.48     /**
  
 49.49      * 得到下载具体信息
  
 50.50      */
  
 51.51     public List<DownloadInfo> getInfos(String urlstr) {
  
 52.52         List<DownloadInfo> list = new ArrayList<DownloadInfo>();
  
 53.53         SQLiteDatabase database = dbHelper.getReadableDatabase();
  
 54.54         String sql = "select thread_id, start_pos, end_pos,compelete_size,url from download_info where url=?";
  
 55.55         Cursor cursor = database.rawQuery(sql, new String[] { urlstr });
  
 56.56         while (cursor.moveToNext()) {
  
 57.57             DownloadInfo info = new DownloadInfo(cursor.getInt(0),
  
 58.58                     cursor.getInt(1), cursor.getInt(2), cursor.getInt(3),
  
 59.59                     cursor.getString(4));
  
 60.60             list.add(info);
  
 61.61         }
  
 62.62         cursor.close();
  
 63.63         return list;
  
 64.64     }
  
 65.65 66.66     /**
  
 67.67      * 更新数据库中的下载信息
  
 68.68      */
  
 69.69     public void updataInfos(int threadId, int compeleteSize, String urlstr) {
  
 70.70         SQLiteDatabase database = dbHelper.getReadableDatabase();
  
 71.71         String sql = "update download_info set compelete_size=? where thread_id=? and url=?";
  
 72.72         Object[] bindArgs = { compeleteSize, threadId, urlstr };
  
 73.73         database.execSQL(sql, bindArgs);
  
 74.74     }
  
 75.75     /**
  
 76.76      * 关闭数据库
  
 77.77      */
  
 78.78     public void closeDb() {
  
 79.79         dbHelper.close();
  
 80.80     }
  
 81.81 82.82     /**
  
 83.83      * 下载完成后删除数据库中的数据
  
 84.84      */
  
 85.85     public void delete(String url) {
  
 86.86         SQLiteDatabase database = dbHelper.getReadableDatabase();
  
 87.87         database.delete("download_info", "url=?", new String[] { url });
  
 88.88         database.close();
  
 89.89     }
  
 90.90 }
 复制代码
 下来就是要进行下载和暂停按钮的响应事件了。具体代码和解释如下。MainActivity:
 01.1 package cn.yj3g.AndroidDownload;
  
 02.  2 03.  3 import java.util.ArrayList;
  
 04.  4 import java.util.HashMap;
  
 05.  5 import java.util.List;
  
 06.  6 import java.util.Map;
  
 07.  7 import android.app.ListActivity;
  
 08.  8 import android.os.Bundle;
  
 09.  9 import android.os.Handler;
  
 10. 10 import android.os.Message;
  
 11. 11 import android.view.View;
  
 12. 12 import android.widget.LinearLayout;
  
 13. 13 import android.widget.LinearLayout.LayoutParams;
  
 14. 14 import android.widget.ProgressBar;
  
 15. 15 import android.widget.SimpleAdapter;
  
 16. 16 import android.widget.TextView;
  
 17. 17 import android.widget.Toast;
  
 18. 18 import cn.yj3g.entity.LoadInfo;
  
 19. 19 import cn.yj3g.service.Downloader;
  
 20. 20 21. 21 public class MainActivity extends ListActivity {
  
 22. 22     // 固定下载的资源路径,这里可以设置网络上的地址
  
 23. 23     private static final String URL = "http://192.168.1.105:8080/struts2_net/";
  
 24. 24     // 固定存放下载的音乐的路径:SD卡目录下
  
 25. 25     private static final String SD_PATH = "/mnt/sdcard/";
  
 26. 26     // 存放各个下载器
  
 27. 27     private Map<String, Downloader> downloaders = new HashMap<String, Downloader>();
  
 28. 28     // 存放与下载器对应的进度条
  
 29. 29     private Map<String, ProgressBar> ProgressBars = new HashMap<String, ProgressBar>();
  
 30. 30     /**
  
 31. 31      * 利用消息处理机制适时更新进度条
  
 32. 32      */
  
 33. 33     private Handler mHandler = new Handler() {
  
 34. 34         public void handleMessage(Message msg) {
  
 35. 35             if (msg.what == 1) {
  
 36. 36                 String url = (String) msg.obj;
  
 37. 37                 int length = msg.arg1;
  
 38. 38                 ProgressBar bar = ProgressBars.get(url);
  
 39. 39                 if (bar != null) {
  
 40. 40                     // 设置进度条按读取的length长度更新
  
 41. 41                     bar.incrementProgressBy(length);
  
 42. 42                     if (bar.getProgress() == bar.getMax()) {
  
 43. 43                         Toast.makeText(MainActivity.this, "下载完成!", 0).show();
  
 44. 44                         // 下载完成后清除进度条并将map中的数据清空
  
 45. 45                         LinearLayout layout = (LinearLayout) bar.getParent();
  
 46. 46                         layout.removeView(bar);
  
 47. 47                         ProgressBars.remove(url);
  
 48. 48                         downloaders.get(url).delete(url);
  
 49. 49                         downloaders.get(url).reset();
  
 50. 50                         downloaders.remove(url);
  
 51. 51 52. 52                     }
  
 53. 53                 }
  
 54. 54             }
  
 55. 55         }
  
 56. 56     };
  
 57. 57     @Override
  
 58. 58     public void onCreate(Bundle savedInstanceState) {
  
 59. 59         super.onCreate(savedInstanceState);
  
 60. 60         setContentView(R.layout.main);
  
 61. 61         showListView();
  
 62. 62     }
  
 63. 63     // 显示listView,这里可以随便添加音乐
  
 64. 64     private void showListView() {
  
 65. 65         List<Map<String, String>> data = new ArrayList<Map<String, String>>();
  
 66. 66         Map<String, String> map = new HashMap<String, String>();
  
 67. 67         map.put("name", "mm.mp3");
  
 68. 68         data.add(map);
  
 69. 69         map = new HashMap<String, String>();
  
 70. 70         map.put("name", "pp.mp3");
  
 71. 71         data.add(map);
  
 72. 72         map = new HashMap<String, String>();
  
 73. 73         map.put("name", "tt.mp3");
  
 74. 74         data.add(map);
  
 75. 75         map = new HashMap<String, String>();
  
 76. 76         map.put("name", "You.mp3");
  
 77. 77         data.add(map);
  
 78. 78         SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.list_item, new String[] { "name" },
  
 79. 79                 new int[] { R.id.tv_resouce_name });
  
 80. 80         setListAdapter(adapter);
  
 81. 81     }
  
 82. 82     /**
  
 83. 83      * 响应开始下载按钮的点击事件
  
 84. 84      */
  
 85. 85     public void startDownload(View v) {
  
 86. 86         // 得到textView的内容
  
 87. 87         LinearLayout layout = (LinearLayout) v.getParent();
  
 88. 88         String musicName = ((TextView) layout.findViewById(R.id.tv_resouce_name)).getText().toString();
  
 89. 89         String urlstr = URL + musicName;
  
 90. 90         String localfile = SD_PATH + musicName;
  
 91. 91         //设置下载线程数为4,这里是我为了方便随便固定的
  
 92. 92         int threadcount = 4;
  
 93. 93         // 初始化一个downloader下载器
  
 94. 94         Downloader downloader = downloaders.get(urlstr);
  
 95. 95         if (downloader == null) {
  
 96. 96             downloader = new Downloader(urlstr, localfile, threadcount, this, mHandler);
  
 97. 97             downloaders.put(urlstr, downloader);
  
 98. 98         }
  
 99. 99         if (downloader.isdownloading())
  
 100.100             return;
  
 101.101         // 得到下载信息类的个数组成集合
  
 102.102         LoadInfo loadInfo = downloader.getDownloaderInfors();
  
 103.103         // 显示进度条
  
 104.104         showProgress(loadInfo, urlstr, v);
  
 105.105         // 调用方法开始下载
  
 106.106         downloader.download();
  
 107.107     }
  
 108.108 109.109     /**
  
 110.110      * 显示进度条
  
 111.111      */
  
 112.112     private void showProgress(LoadInfo loadInfo, String url, View v) {
  
 113.113         ProgressBar bar = ProgressBars.get(url);
  
 114.114         if (bar == null) {
  
 115.115             bar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);
  
 116.116             bar.setMax(loadInfo.getFileSize());
  
 117.117             bar.setProgress(loadInfo.getComplete());
  
 118.118             ProgressBars.put(url, bar);
  
 119.119             LinearLayout.LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, 5);
  
 120.120             ((LinearLayout) ((LinearLayout) v.getParent()).getParent()).addView(bar, params);
  
 121.121         }
  
 122.122     }
  
 123.123     /**
  
 124.124      * 响应暂停下载按钮的点击事件
  
 125.125      */
  
 126.126     public void pauseDownload(View v) {
  
 127.127         LinearLayout layout = (LinearLayout) v.getParent();
  
 128.128         String musicName = ((TextView) layout.findViewById(R.id.tv_resouce_name)).getText().toString();
  
 129.129         String urlstr = URL + musicName;
  
 130.130         downloaders.get(urlstr).pause();
  
 131.131     }
  
 132.132 }


复制代码最后我们要在清单文件中添加权限,一个是访问网络的权限,一个是往SD卡写数据的权限。代码如下:

01.<uses-permission android:name="android.permission.INTERNET"/>
 
02. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
复制代码
这样我们就实现了文件的断点续传下载功能。具体效果图如下:

 

 

 

 

 

 

下载, Android