国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

Android多線程斷點(diǎn)續(xù)傳下載示例詳解

2019-12-12 01:43:56
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

一、概述

在上一篇博文《Android多線程下載示例》中,我們講解了如何實(shí)現(xiàn)Android的多線程下載功能,通過(guò)將整個(gè)文件分成多個(gè)數(shù)據(jù)塊,開(kāi)啟多個(gè)線程,讓每個(gè)線程分別下載一個(gè)相應(yīng)的數(shù)據(jù)塊來(lái)實(shí)現(xiàn)多線程下載的功能。多線程下載中,可以將下載這個(gè)耗時(shí)的操作放在子線程中執(zhí)行,即不阻塞主線程,又符合Android開(kāi)發(fā)的設(shè)計(jì)規(guī)范。

但是當(dāng)下載的過(guò)程當(dāng)中突然出現(xiàn)手機(jī)卡死,或者網(wǎng)絡(luò)中斷,手機(jī)電量不足關(guān)機(jī)的現(xiàn)象,這時(shí),當(dāng)手機(jī)可以正常使用后,如果重新下載文件,似乎不太符合大多數(shù)用戶的心理期望,那如何實(shí)現(xiàn)當(dāng)手機(jī)可以正常聯(lián)網(wǎng)時(shí),基于上次斷網(wǎng)時(shí)下載的數(shù)據(jù)來(lái)下載呢?這就是所謂的斷點(diǎn)下載了。這篇文章主要是講解如何實(shí)現(xiàn)斷點(diǎn)下載的功能。

本文講解的Android斷點(diǎn)下載是基于上一篇文章《Android多線程下載示例》 ,本示例是在上一示例的基礎(chǔ)上通過(guò)在下載的過(guò)程中,將下載的信息保存到Andoid系統(tǒng)自帶的數(shù)據(jù)庫(kù)SQLite中,當(dāng)手機(jī)出現(xiàn)異常情況而斷開(kāi)網(wǎng)絡(luò)時(shí),由于數(shù)據(jù)庫(kù)中記錄了上次下載的數(shù)據(jù)信息,當(dāng)手機(jī)再次聯(lián)網(wǎng)時(shí),讀取數(shù)據(jù)庫(kù)中的信息,從上次斷開(kāi)下載的地方繼續(xù)下載數(shù)據(jù)。好,不多說(shuō)了,進(jìn)入正文。

二、服務(wù)端準(zhǔn)備

服務(wù)端的實(shí)現(xiàn)很簡(jiǎn)單,這里為了使下載的文件大些,我在網(wǎng)絡(luò)上下載了有道詞典來(lái)作為要下載的測(cè)試資源。將它放置在項(xiàng)目的WebContent目錄下,并將項(xiàng)目發(fā)布在Tomcat服務(wù)器中,具體如下圖所示:

就這樣,服務(wù)端算是弄好了,怎么樣?很簡(jiǎn)單吧?相信大家都會(huì)的!

三、Android實(shí)現(xiàn)

Android實(shí)現(xiàn)部分是本文的重點(diǎn),這里我們從布局開(kāi)始由淺入深慢慢講解,這里我們通過(guò)Activity來(lái)顯示程序的界面,以SQLite數(shù)據(jù)庫(kù)來(lái)保存下載的信息,通過(guò)ContentProvider來(lái)操作保存的記錄信息,通過(guò)Handler和Message機(jī)制將子線程中的數(shù)據(jù)傳遞到主線程來(lái)更新UI顯示。同時(shí)通過(guò)自定義監(jiān)聽(tīng)器來(lái)實(shí)現(xiàn)對(duì)UI顯示更新的監(jiān)聽(tīng)操作。

1、布局實(shí)現(xiàn)

布局基本上和上一博文中的布局一樣,沒(méi)有什么大的變動(dòng),界面上自上而下放置一個(gè)TextView,用來(lái)提示文本框中輸入的信息,一個(gè)文本框用來(lái)輸入網(wǎng)絡(luò)中下載文件的路徑,一個(gè)Button按鈕,點(diǎn)擊下載文件,一個(gè)ProgressBar顯示下載進(jìn)度,一個(gè)TextView顯示下載的百分比。

具體布局內(nèi)容如下:

<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監(jiān)聽(tīng)器接口

新建自定義ProgressBarListener監(jiān)聽(tīng)器接口,這個(gè)接口中定義兩個(gè)方法,void getMax(int length)用來(lái)獲取下載文件的長(zhǎng)度,void getDownload(int length);用來(lái)獲取每次下載的長(zhǎng)度,這個(gè)方法中主要是在多線程中調(diào)用,子線程中獲取到的數(shù)據(jù)傳遞到這兩個(gè)接口方法中,然后在這兩個(gè)接口方法中通過(guò)Handler將相應(yīng)的長(zhǎng)度信息傳遞到主線程,更新界面顯示信息。

具體代碼實(shí)現(xiàn)如下:

package com.example.inter;  /**  * 自定義進(jìn)度條監(jiān)聽(tīng)器  * @author liuyazhuang  *  */ public interface ProgressBarListener {  /**  * 獲取文件的長(zhǎng)度  * @param length  */  void getMax(int length);  /**  * 獲取每次下載的長(zhǎng)度  * @param length  */  void getDownload(int length); } 

3.定義數(shù)據(jù)庫(kù)的相關(guān)信息類DownloadDBHelper

在這個(gè)實(shí)例中,我們將數(shù)據(jù)庫(kù)的名稱定義為download.db,我們需要保存主鍵id,文件下載后要保存的路徑,每個(gè)線程的標(biāo)識(shí)id,每個(gè)線程下載的文件數(shù)據(jù)塊大小,所以,在創(chuàng)建的數(shù)據(jù)表中共有_id, path,threadid,downloadlength,詳情見(jiàn)下圖

DownloadDBHelper實(shí)現(xiàn)的具體代碼如下:

package com.example.db;  import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper;  /**  * 數(shù)據(jù)庫(kù)相關(guān)類  * @author liuyazhuang  *  */ public class DownloadDBHelper extends SQLiteOpenHelper {  /**  * 數(shù)據(jù)庫(kù)名稱  */  private static final String NAME = "download.db";  /**  * 原有的構(gòu)造方法  * @param context  * @param name  * @param factory  * @param version  */  public DownloadDBHelper(Context context, String name,  CursorFactory factory, int version) {  super(context, name, factory, version);  }  /**  * 重載構(gòu)造方法  * @param context  */  public DownloadDBHelper(Context context){  super(context, NAME, null, 1);  }   /**  * 創(chuàng)建數(shù)據(jù)庫(kù)時(shí)調(diào)用  */  @Override  public void onCreate(SQLiteDatabase db) {  db.execSQL("create table download(_id integer primary key autoincrement," +   "path text," +   "threadid integer," +   "downloadlength integer)");   }  /**  * 更新數(shù)據(jù)庫(kù)時(shí)調(diào)用  */  @Override  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {   }  } 

 4、創(chuàng)建DownloadProvider類

DownloadProvider類繼承自ContentProvider,提供操作數(shù)據(jù)庫(kù)的方法,在這個(gè)類中,通過(guò)UriMatcher類匹配要操作的數(shù)據(jù)庫(kù),通過(guò)DownloadDBHelper對(duì)象來(lái)得到一個(gè)具體數(shù)據(jù)庫(kù)實(shí)例,來(lái)對(duì)相應(yīng)的數(shù)據(jù)庫(kù)進(jìn)行增、刪、改、查操作。
具體實(shí)現(xiàn)如下代碼所示:

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實(shí)例  * @author liuyazhuang  *  */ public class DownloadProvider extends ContentProvider {  //實(shí)例化UriMatcher對(duì)象  private static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);  //配置訪問(wèn)規(guī)則  private static final String AUTHORITY = "download";  //自定義常量  private static final int DOWANLOAD = 10;  static{  //添加匹配的規(guī)則  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、創(chuàng)建DownloadInfo實(shí)體類

為了使程序更加面向?qū)ο蠡@里我們建立DownloadInfo實(shí)體類來(lái)對(duì)數(shù)據(jù)庫(kù)中的數(shù)據(jù)進(jìn)行封裝,DownloadInfo實(shí)體類中的數(shù)據(jù)字段與數(shù)據(jù)庫(kù)中的字段相對(duì)應(yīng)
具體實(shí)現(xiàn)代碼如下:

package com.example.domain;  /**  * 支持?jǐn)帱c(diǎn)續(xù)傳時(shí),  * 要保存到數(shù)據(jù)庫(kù)的信息  * @author liuyazhuang  *  */ public class DownloadInfo {  //主鍵id  private int _id;  //保存路徑  private String path;  //線程的標(biāo)識(shí)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、定義外界調(diào)用的操作數(shù)據(jù)庫(kù)的方法類DownloadDao

DownloadDao類中封裝了一系列操作數(shù)據(jù)庫(kù)的方法,這個(gè)類不是直接操作數(shù)據(jù)庫(kù)對(duì)象,而是通過(guò)ContentResolver這個(gè)對(duì)象來(lái)調(diào)用DownloadProvider中的方法來(lái)實(shí)現(xiàn)操作數(shù)據(jù)庫(kù)的功能,這里用到了ContentResolver與ContentProvider這兩個(gè)Android中非常重要的類。ContentProvider即內(nèi)容提供者,主要是向外提供數(shù)據(jù),簡(jiǎn)單理解就是一個(gè)應(yīng)用程序可以通過(guò)ContentProvider向外提供操作本應(yīng)用程序的接口,其他應(yīng)用程序可以調(diào)用ContentProvider提供的接口來(lái)操作本應(yīng)用程序的數(shù)據(jù)。ContentResolver內(nèi)容接接收者,它可以接收ContentProvider的向外提供的數(shù)據(jù)。
具體代碼實(shí)現(xiàn)如下:

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對(duì)象  */  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;  }   /**  * 計(jì)算所有的下載長(zhǎng)度  * @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;  }  /**  * 計(jì)算每個(gè)線程的下載長(zhǎng)度  * @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

這里通過(guò)繼承Thread的方式來(lái)實(shí)現(xiàn)自定義線程操作,在這個(gè)類中主要是實(shí)現(xiàn)文件的下載操作,在這個(gè)類中,定義了一系列與下載有關(guān)的實(shí)例變量來(lái)控制下載的數(shù)據(jù),通過(guò)自定義監(jiān)聽(tīng)器ProgressBarListener中的void getDownload(int length)方法來(lái)跟新界面顯示的進(jìn)度信息,同時(shí)通過(guò)調(diào)用DownloadDao的方法來(lái)記錄和更新數(shù)據(jù)的下載信息。
具體實(shí)現(xiàn)代碼如下:

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;  //下載的進(jìn)度條更新的監(jiān)聽(tīng)器  private ProgressBarListener listener;  //每條線程下載的數(shù)據(jù)量  private int block;  //下載的開(kāi)始位置  private int startPosition;  //下載的結(jié)束位置  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;  //創(chuàng)建RandomAccessFile對(duì)象  RandomAccessFile accessFile = new RandomAccessFile(file, "rwd");  //跳轉(zhuǎn)到開(kāi)始位置  accessFile.seek(startPosition);  URL url = new URL(path);  //打開(kāi)http鏈接  HttpURLConnection conn = (HttpURLConnection) url.openConnection();  //設(shè)置超時(shí)時(shí)間  conn.setConnectTimeout(5000);  //指定請(qǐng)求方式為GET方式  conn.setRequestMethod("GET");  //指定下載的位置  conn.setRequestProperty("Range", "bytes="+startPosition + "-" + endPosition);  //不用再去判斷狀態(tài)碼是否為200  InputStream in = conn.getInputStream();  byte[] buffer = new byte[1024];  int len = 0;  //該線程下載的總數(shù)據(jù)量  int count = length;  while((len = in.read(buffer)) != -1){  accessFile.write(buffer, 0, len);  //更新下載進(jìn)度  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

這個(gè)類主要是對(duì)下載過(guò)程的管理,包括下載設(shè)置下載后文件要保存的位置,計(jì)算多線程中每個(gè)線程的數(shù)據(jù)下載量等等,同時(shí)相比《Android多線程下載示例》一文中,它多了多下載數(shù)據(jù)的記錄與更新操作。
具體實(shí)現(xiàn)代碼如下:

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 {  //下載線程的數(shù)量  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:自定義的下載文件監(jiān)聽(tīng)接口  * @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();  //設(shè)置進(jìn)度條的最大長(zhǎng)度  listener.getMax(filesize);  //判斷下載記錄是否存在  boolean ret = downloadDao.isExist(path);  if(ret){  //得到下載的總長(zhǎng)度,設(shè)置進(jìn)度條的刻度  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);  }  }  //創(chuàng)建一個(gè)和服務(wù)器大小一樣的文件  file = new File(Environment.getExternalStorageDirectory(), this.getFileName(path));  RandomAccessFile accessFile = new RandomAccessFile(file, "rwd");  accessFile.setLength(filesize);  //要關(guān)閉RandomAccessFile對(duì)象  accessFile.close();   //計(jì)算出每條線程下載的數(shù)據(jù)量  int block = filesize % TREAD_SIZE == 0 ? (filesize / TREAD_SIZE) : (filesize / TREAD_SIZE +1 );   //開(kāi)啟線程下載  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

在這個(gè)類中首先,找到頁(yè)面中的各個(gè)控件,實(shí)現(xiàn)Button按鈕的onClick事件,在onClick事件中開(kāi)啟一個(gè)線程進(jìn)行下載操作,同時(shí)子線程中獲取到的數(shù)據(jù),通過(guò)handler與Message機(jī)制傳遞到主線程,更新界面顯示,利用DownloadDao類中的方法來(lái)記錄和更新下載數(shù)據(jù)。
具體實(shí)現(xiàn)代碼如下:

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整個(gè)應(yīng)用程序的入口  * @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;  //設(shè)置進(jìn)度條的最大值  pb.setMax(max);  break;  case UPDATE_PROGRESS:  //獲取當(dāng)前下載的長(zhǎng)度  int currentprogress = pb.getProgress();  //獲取新下載的長(zhǎng)度  int len = (Integer) msg.obj;  //計(jì)算當(dāng)前總下載長(zhǎng)度  int crrrentTotalProgress = currentprogress + len;  pb.setProgress(crrrentTotalProgress);    //獲取總大小  int maxProgress = pb.getMax();  //計(jì)算百分比  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、增加權(quán)限

最后,別忘了給應(yīng)用授權(quán),這里要用到Android聯(lián)網(wǎng)授權(quán)和向SD卡中寫(xiě)入文件的權(quán)限。
具體實(shí)現(xiàn)如下:

<?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> 

四、運(yùn)行效果

如上:實(shí)現(xiàn)了Android中的斷點(diǎn)下載功能。
提醒:大家可以到這個(gè)鏈接來(lái)獲取完整的Android斷點(diǎn)下載示例源碼

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 武乡县| 囊谦县| 肥西县| 逊克县| 吉林市| 偏关县| 陇川县| 濮阳县| 铜川市| 高陵县| 安宁市| 新丰县| 格尔木市| 利辛县| 章丘市| 中山市| 温州市| 高平市| 永福县| 牡丹江市| 焉耆| 翼城县| 三河市| 中方县| 宣恩县| 额济纳旗| 谢通门县| 江津市| 蒙自县| 印江| 乌兰县| 紫阳县| 永年县| 乌拉特后旗| 合山市| 东阿县| 桂平市| 开江县| 平阴县| 北流市| 信宜市|