一、概述
在上一篇博文《Android多線程下載示例》中,我們講解了如何實現Android的多線程下載功能,通過將整個文件分成多個數據塊,開啟多個線程,讓每個線程分別下載一個相應的數據塊來實現多線程下載的功能。多線程下載中,可以將下載這個耗時的操作放在子線程中執行,即不阻塞主線程,又符合Android開發的設計規范。
但是當下載的過程當中突然出現手機卡死,或者網絡中斷,手機電量不足關機的現象,這時,當手機可以正常使用后,如果重新下載文件,似乎不太符合大多數用戶的心理期望,那如何實現當手機可以正常聯網時,基于上次斷網時下載的數據來下載呢?這就是所謂的斷點下載了。這篇文章主要是講解如何實現斷點下載的功能。
本文講解的Android斷點下載是基于上一篇文章《Android多線程下載示例》 ,本示例是在上一示例的基礎上通過在下載的過程中,將下載的信息保存到Andoid系統自帶的數據庫SQLite中,當手機出現異常情況而斷開網絡時,由于數據庫中記錄了上次下載的數據信息,當手機再次聯網時,讀取數據庫中的信息,從上次斷開下載的地方繼續下載數據。好,不多說了,進入正文。
二、服務端準備
服務端的實現很簡單,這里為了使下載的文件大些,我在網絡上下載了有道詞典來作為要下載的測試資源。將它放置在項目的WebContent目錄下,并將項目發布在Tomcat服務器中,具體如下圖所示:
就這樣,服務端算是弄好了,怎么樣?很簡單吧?相信大家都會的!
三、Android實現
Android實現部分是本文的重點,這里我們從布局開始由淺入深慢慢講解,這里我們通過Activity來顯示程序的界面,以SQLite數據庫來保存下載的信息,通過ContentProvider來操作保存的記錄信息,通過Handler和Message機制將子線程中的數據傳遞到主線程來更新UI顯示。同時通過自定義監聽器來實現對UI顯示更新的監聽操作。
1、布局實現
布局基本上和上一博文中的布局一樣,沒有什么大的變動,界面上自上而下放置一個TextView,用來提示文本框中輸入的信息,一個文本框用來輸入網絡中下載文件的路徑,一個Button按鈕,點擊下載文件,一個ProgressBar顯示下載進度,一個TextView顯示下載的百分比。
具體布局內容如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context=".MainActivity" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="下載路徑" /> <EditText android:id="@+id/ed_path" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="http://192.168.0.170:8080/web/youdao.exe"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="下載" android:onClick="download"/> <ProgressBar android:id="@+id/pb" android:layout_width="match_parent" android:layout_height="wrap_content" style="@android:style/Widget.ProgressBar.Horizontal"/> <TextView android:id="@+id/tv_info" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="下載:0%"/> </LinearLayout>
2、自定義ProgressBarListener監聽器接口
新建自定義ProgressBarListener監聽器接口,這個接口中定義兩個方法,void getMax(int length)用來獲取下載文件的長度,void getDownload(int length);用來獲取每次下載的長度,這個方法中主要是在多線程中調用,子線程中獲取到的數據傳遞到這兩個接口方法中,然后在這兩個接口方法中通過Handler將相應的長度信息傳遞到主線程,更新界面顯示信息。
具體代碼實現如下:
package com.example.inter;  /**  * 自定義進度條監聽器  * @author liuyazhuang  *  */ public interface ProgressBarListener {  /**  * 獲取文件的長度  * @param length  */  void getMax(int length);  /**  * 獲取每次下載的長度  * @param length  */  void getDownload(int length); } 3.定義數據庫的相關信息類DownloadDBHelper
在這個實例中,我們將數據庫的名稱定義為download.db,我們需要保存主鍵id,文件下載后要保存的路徑,每個線程的標識id,每個線程下載的文件數據塊大小,所以,在創建的數據表中共有_id, path,threadid,downloadlength,詳情見下圖
	
DownloadDBHelper實現的具體代碼如下:
package com.example.db;  import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper;  /**  * 數據庫相關類  * @author liuyazhuang  *  */ public class DownloadDBHelper extends SQLiteOpenHelper {  /**  * 數據庫名稱  */  private static final String NAME = "download.db";  /**  * 原有的構造方法  * @param context  * @param name  * @param factory  * @param version  */  public DownloadDBHelper(Context context, String name,  CursorFactory factory, int version) {  super(context, name, factory, version);  }  /**  * 重載構造方法  * @param context  */  public DownloadDBHelper(Context context){  super(context, NAME, null, 1);  }   /**  * 創建數據庫時調用  */  @Override  public void onCreate(SQLiteDatabase db) {  db.execSQL("create table download(_id integer primary key autoincrement," +   "path text," +   "threadid integer," +   "downloadlength integer)");   }  /**  * 更新數據庫時調用  */  @Override  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {   }  } 4、創建DownloadProvider類
	DownloadProvider類繼承自ContentProvider,提供操作數據庫的方法,在這個類中,通過UriMatcher類匹配要操作的數據庫,通過DownloadDBHelper對象來得到一個具體數據庫實例,來對相應的數據庫進行增、刪、改、查操作。
	具體實現如下代碼所示:
package com.example.provider;  import com.example.db.DownloadDBHelper;  import android.content.ContentProvider; import android.content.ContentValues; import android.content.UriMatcher; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.net.Uri;  /**  * 自定義ContentProvider實例  * @author liuyazhuang  *  */ public class DownloadProvider extends ContentProvider {  //實例化UriMatcher對象  private static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);  //配置訪問規則  private static final String AUTHORITY = "download";  //自定義常量  private static final int DOWANLOAD = 10;  static{  //添加匹配的規則  matcher.addURI(AUTHORITY, "download", DOWANLOAD);  }  private SQLiteOpenHelper mOpenHelper;  @Override  public boolean onCreate() {  mOpenHelper = new DownloadDBHelper(getContext());  return false;  }   @Override  public Cursor query(Uri uri, String[] projection, String selection,  String[] selectionArgs, String sortOrder) {  // TODO Auto-generated method stub  Cursor ret = null;  SQLiteDatabase db = mOpenHelper.getReadableDatabase();  int code = matcher.match(uri);  switch (code) {  case DOWANLOAD:  ret = db.query("download", projection, selection, selectionArgs, null, null, sortOrder);  break;   default:  break;  }  return ret;  }   @Override  public String getType(Uri uri) {  // TODO Auto-generated method stub  return null;  }   @Override  public Uri insert(Uri uri, ContentValues values) {  // TODO Auto-generated method stub  SQLiteDatabase db = mOpenHelper.getWritableDatabase();  int code = matcher.match(uri);  switch (code) {  case DOWANLOAD:  db.insert("download", "_id", values);  break;   default:  break;  }  return null;  }   @Override  public int delete(Uri uri, String selection, String[] selectionArgs) {  SQLiteDatabase db = mOpenHelper.getWritableDatabase();  int code = matcher.match(uri);  switch (code) {  case DOWANLOAD:  db.delete("download", selection, selectionArgs);  break;   default:  break;  }  return 0;  }   @Override  public int update(Uri uri, ContentValues values, String selection,  String[] selectionArgs) {  SQLiteDatabase db = mOpenHelper.getWritableDatabase();  int code = matcher.match(uri);  switch (code) {  case DOWANLOAD:  db.update("download", values, selection, selectionArgs);  break;   default:  break;  }  return 0;  }  } 5、創建DownloadInfo實體類
	為了使程序更加面向對象化,這里我們建立DownloadInfo實體類來對數據庫中的數據進行封裝,DownloadInfo實體類中的數據字段與數據庫中的字段相對應
	具體實現代碼如下:
package com.example.domain;  /**  * 支持斷點續傳時,  * 要保存到數據庫的信息  * @author liuyazhuang  *  */ public class DownloadInfo {  //主鍵id  private int _id;  //保存路徑  private String path;  //線程的標識id  private String threadId;  //下載文件的大小  private int downloadSize;   public DownloadInfo() {  super();  }   public DownloadInfo(int _id, String path, String threadId, int downloadSize) {  super();  this._id = _id;  this.path = path;  this.threadId = threadId;  this.downloadSize = downloadSize;  }   public int get_id() {  return _id;  }  public void set_id(int _id) {  this._id = _id;  }  public String getPath() {  return path;  }  public void setPath(String path) {  this.path = path;  }  public String getThreadId() {  return threadId;  }  public void setThreadId(String threadId) {  this.threadId = threadId;  }  public int getDownloadSize() {  return downloadSize;  }  public void setDownloadSize(int downloadSize) {  this.downloadSize = downloadSize;  } } 6、定義外界調用的操作數據庫的方法類DownloadDao
	DownloadDao類中封裝了一系列操作數據庫的方法,這個類不是直接操作數據庫對象,而是通過ContentResolver這個對象來調用DownloadProvider中的方法來實現操作數據庫的功能,這里用到了ContentResolver與ContentProvider這兩個Android中非常重要的類。ContentProvider即內容提供者,主要是向外提供數據,簡單理解就是一個應用程序可以通過ContentProvider向外提供操作本應用程序的接口,其他應用程序可以調用ContentProvider提供的接口來操作本應用程序的數據。ContentResolver內容接接收者,它可以接收ContentProvider的向外提供的數據。
	具體代碼實現如下:
package com.example.dao;  import android.content.ContentResolver; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.net.Uri;  import com.example.domain.DownloadInfo;  /**  * 保存下載文件信息的dao類  * @author liuyazhuang  *  */ public class DownloadDao {   /**  * ContentResolver對象  */  private ContentResolver cr;   public DownloadDao(Context context){  this.cr = context.getContentResolver();  }  /**  * 保存下載信息記錄  * @param info  */  public void save(DownloadInfo info){  Uri uri = Uri.parse("content://download/download");  ContentValues values = new ContentValues();  values.put("path", info.getPath());  values.put("threadid", info.getThreadId());  cr.insert(uri, values);  }   /**  * 更新下載信息記錄  * @param info  */  public void update(DownloadInfo info){  Uri uri = Uri.parse("content://download/download");  ContentValues values = new ContentValues();  values.put("downloadlength", info.getDownloadSize());  values.put("threadid", info.getThreadId());  cr.update(uri, values, " path = ? and threadid = ? ", new String[]{info.getPath(), info.getThreadId()});  }  /**  * 刪除下載信息記錄  * @param info  */  public void delete(DownloadInfo info){  Uri uri = Uri.parse("content://download/download");  cr.delete(uri, " path = ? and threadid = ? ", new String[]{info.getPath(), info.getThreadId()});  }  /**  * 刪除下載信息記錄  * @param info  */  public void delete(String path){  Uri uri = Uri.parse("content://download/download");  cr.delete(uri, " path = ? ", new String[]{path});  }   /**  * 判斷是否有下載記錄  * @param path  * @return  */  public boolean isExist(String path){  boolean result = false;  Uri uri = Uri.parse("content://download/download");  Cursor cursor = cr.query(uri, null, " path = ? ", new String[]{path}, null);  if(cursor.moveToNext()){  result = true;  }  cursor.close();  return result;  }   /**  * 計算所有的下載長度  * @param path  * @return  */  public int queryCount(String path){  int count = 0;  Uri uri = Uri.parse("content://download/download");  Cursor cursor = cr.query(uri, new String[]{"downloadlength"}, " path = ? ", new String[]{path}, null);  while(cursor.moveToNext()){  int len = cursor.getInt(0);  count += len;  }  cursor.close();  return count;  }  /**  * 計算每個線程的下載長度  * @param path  * @return  */  public int query(DownloadInfo info){  int count = 0;  Uri uri = Uri.parse("content://download/download");  Cursor cursor = cr.query(uri, new String[]{"downloadlength"}, " path = ? and threadid = ?", new String[]{info.getPath(), info.getThreadId()}, null);  while(cursor.moveToNext()){  int len = cursor.getInt(0);  count += len;  }  cursor.close();  return count;  } } 7、自定義線程類DownThread
	這里通過繼承Thread的方式來實現自定義線程操作,在這個類中主要是實現文件的下載操作,在這個類中,定義了一系列與下載有關的實例變量來控制下載的數據,通過自定義監聽器ProgressBarListener中的void getDownload(int length)方法來跟新界面顯示的進度信息,同時通過調用DownloadDao的方法來記錄和更新數據的下載信息。
	具體實現代碼如下:
package com.example.download;  import java.io.File; import java.io.InputStream; import java.io.RandomAccessFile; import java.net.HttpURLConnection; import java.net.URL;  import android.content.Context;  import com.example.dao.DownloadDao; import com.example.domain.DownloadInfo; import com.example.inter.ProgressBarListener;  /**  * 自定義線程類  * @author liuyazhuang  *  */ public class DownloadThread extends Thread {  //下載的線程id  private int threadId;  //下載的文件路徑  private String path;  //保存的文件  private File file;  //下載的進度條更新的監聽器  private ProgressBarListener listener;  //每條線程下載的數據量  private int block;  //下載的開始位置  private int startPosition;  //下載的結束位置  private int endPosition;   private DownloadDao downloadDao;   public DownloadThread(int threadId, String path, File file, ProgressBarListener listener, int block, Context context) {  this.threadId = threadId;  this.path = path;  this.file = file;  this.listener = listener;  this.block = block;  this.downloadDao = new DownloadDao(context);  this.startPosition = threadId * block;  this.endPosition = (threadId + 1) * block - 1;  }   @Override  public void run() {  super.run();  try {  //判斷該線程是否有下載記錄  DownloadInfo info = new DownloadInfo();  info.setPath(path);  info.setThreadId(String.valueOf(threadId));  int length = downloadDao.query(info);  startPosition += length;  //創建RandomAccessFile對象  RandomAccessFile accessFile = new RandomAccessFile(file, "rwd");  //跳轉到開始位置  accessFile.seek(startPosition);  URL url = new URL(path);  //打開http鏈接  HttpURLConnection conn = (HttpURLConnection) url.openConnection();  //設置超時時間  conn.setConnectTimeout(5000);  //指定請求方式為GET方式  conn.setRequestMethod("GET");  //指定下載的位置  conn.setRequestProperty("Range", "bytes="+startPosition + "-" + endPosition);  //不用再去判斷狀態碼是否為200  InputStream in = conn.getInputStream();  byte[] buffer = new byte[1024];  int len = 0;  //該線程下載的總數據量  int count = length;  while((len = in.read(buffer)) != -1){  accessFile.write(buffer, 0, len);  //更新下載進度  listener.getDownload(len);  count += len;  info.setDownloadSize(count);  //更新下載的信息  downloadDao.update(info);  }  accessFile.close();  in.close();  } catch (Exception e) {  // TODO: handle exception  e.printStackTrace();  }  } } 8、新建下載的管理類DownloadManager
	這個類主要是對下載過程的管理,包括下載設置下載后文件要保存的位置,計算多線程中每個線程的數據下載量等等,同時相比《Android多線程下載示例》一文中,它多了多下載數據的記錄與更新操作。
	具體實現代碼如下:
package com.example.download;  import java.io.File; import java.io.RandomAccessFile; import java.net.HttpURLConnection; import java.net.URL;  import android.content.Context; import android.os.Environment;  import com.example.dao.DownloadDao; import com.example.domain.DownloadInfo; import com.example.inter.ProgressBarListener;  /**  * 文件下載管理器  * @author liuyazhuang  *  */ public class DownloadManager {  //下載線程的數量  private static final int TREAD_SIZE = 3;  private File file;  private DownloadDao downloadDao;  private Context context;  public DownloadManager(Context context) {  this.context = context;  this.downloadDao = new DownloadDao(context);  }   /**  * 下載文件的方法  * @param path:下載文件的路徑  * @param listener:自定義的下載文件監聽接口  * @throws Exception  */  public void download(String path, ProgressBarListener listener) throws Exception{  URL url = new URL(path);  HttpURLConnection conn = (HttpURLConnection) url.openConnection();  conn.setConnectTimeout(5000);  conn.setRequestMethod("GET");  if(conn.getResponseCode() == 200){  int filesize = conn.getContentLength();  //設置進度條的最大長度  listener.getMax(filesize);  //判斷下載記錄是否存在  boolean ret = downloadDao.isExist(path);  if(ret){  //得到下載的總長度,設置進度條的刻度  int count = downloadDao.queryCount(path);  listener.getDownload(count);  }else{  //保存下載記錄  for(int i = 0; i < filesize; i++){   DownloadInfo info = new DownloadInfo();   info.setPath(path);   info.setThreadId(String.valueOf(i));   //保存下載的記錄信息   downloadDao.save(info);  }  }  //創建一個和服務器大小一樣的文件  file = new File(Environment.getExternalStorageDirectory(), this.getFileName(path));  RandomAccessFile accessFile = new RandomAccessFile(file, "rwd");  accessFile.setLength(filesize);  //要關閉RandomAccessFile對象  accessFile.close();   //計算出每條線程下載的數據量  int block = filesize % TREAD_SIZE == 0 ? (filesize / TREAD_SIZE) : (filesize / TREAD_SIZE +1 );   //開啟線程下載  for(int i = 0; i < TREAD_SIZE; i++){  new DownloadThread(i, path, file, listener, block, context).start();  }  }  }   /**  * 截取路徑中的文件名稱  * @param path:要截取文件名稱的路徑  * @return:截取到的文件名稱  */  private String getFileName(String path){  return path.substring(path.lastIndexOf("/") + 1);  } } 9、完善MainActivity
	在這個類中首先,找到頁面中的各個控件,實現Button按鈕的onClick事件,在onClick事件中開啟一個線程進行下載操作,同時子線程中獲取到的數據,通過handler與Message機制傳遞到主線程,更新界面顯示,利用DownloadDao類中的方法來記錄和更新下載數據。
	具體實現代碼如下:
package com.example.multi;  import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.Menu; import android.view.View; import android.widget.EditText; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast;  import com.example.dao.DownloadDao; import com.example.download.DownloadManager; import com.example.inter.ProgressBarListener;  /**  * MainActivity整個應用程序的入口  * @author liuyazhuang  *  */ public class MainActivity extends Activity {   protected static final int ERROR_DOWNLOAD = 0;  protected static final int SET_PROGRESS_MAX = 1;  protected static final int UPDATE_PROGRESS = 2;   private EditText ed_path;  private ProgressBar pb;  private TextView tv_info;  private DownloadManager manager;  private DownloadDao downloadDao;   //handler操作  private Handler mHandler = new Handler(){   public void handleMessage(android.os.Message msg) {  switch (msg.what) {  case ERROR_DOWNLOAD:  //提示用戶下載失敗  Toast.makeText(MainActivity.this, "下載失敗", Toast.LENGTH_SHORT).show();  break;  case SET_PROGRESS_MAX:  //得到最大值  int max = (Integer) msg.obj;  //設置進度條的最大值  pb.setMax(max);  break;  case UPDATE_PROGRESS:  //獲取當前下載的長度  int currentprogress = pb.getProgress();  //獲取新下載的長度  int len = (Integer) msg.obj;  //計算當前總下載長度  int crrrentTotalProgress = currentprogress + len;  pb.setProgress(crrrentTotalProgress);    //獲取總大小  int maxProgress = pb.getMax();  //計算百分比  float value = (float)currentprogress / (float)maxProgress;  int percent = (int) (value * 100);  //顯示下載的百分比  tv_info.setText("下載:"+percent+"%");    if(maxProgress == crrrentTotalProgress){   //刪除下載記錄   downloadDao.delete(ed_path.getText().toString());  }  break;  default:  break;  }  };  };  @Override  protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.activity_main);  this.ed_path = (EditText) super.findViewById(R.id.ed_path);  this.pb = (ProgressBar) super.findViewById(R.id.pb);  this.tv_info = (TextView) super.findViewById(R.id.tv_info);  this.manager = new DownloadManager(this);  this.downloadDao = new DownloadDao(this);  }   @Override  public boolean onCreateOptionsMenu(Menu menu) {  // Inflate the menu; this adds items to the action bar if it is present.  getMenuInflater().inflate(R.menu.main, menu);  return true;  }   public void download(View v){  final String path = ed_path.getText().toString();  //下載  new Thread(new Runnable() {  @Override  public void run() {  // TODO Auto-generated method stub  try {   manager.download(path, new ProgressBarListener() {   @Override   public void getMax(int length) {   // TODO Auto-generated method stub   Message message = new Message();   message.what = SET_PROGRESS_MAX;   message.obj = length;   mHandler.sendMessage(message);   }     @Override   public void getDownload(int length) {   // TODO Auto-generated method stub   Message message = new Message();   message.what = UPDATE_PROGRESS;   message.obj = length;   mHandler.sendMessage(message);   }   });  } catch (Exception e) {   // TODO: handle exception   e.printStackTrace();   Message message = new Message();   message.what = ERROR_DOWNLOAD;   mHandler.sendMessage(message);  }  }  }).start();  } } 10、增加權限
	最后,別忘了給應用授權,這里要用到Android聯網授權和向SD卡中寫入文件的權限。
	具體實現如下:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.multi" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.multi.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <provider android:name="com.example.provider.DownloadProvider" android:authorities="download"></provider> </application> </manifest>
四、運行效果
	
	
	
	如上:實現了Android中的斷點下載功能。
	提醒:大家可以到這個鏈接來獲取完整的Android斷點下載示例源碼
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。
新聞熱點
疑難解答