this blog is the last article in the AsyncTask download series, with a blog about the broken point and multi thread downloading, which was implemented on the basis of the first two, and interesting to see.

1, AsyncTask implement a breakpoint to extend

two, AsyncTask implement multi thread breakpoint download and implement a single interface, because there is only one interface, so it does not put the download in it, but direct Created in Activity. In formal projects, download is placed in Service, and then updates progress through BroadCast notification interface. Before the code on

, let's take a look at the running diagram of demo.

, we see the code, where each file is downloaded to define a Downloador to manage all threads to download the file (pause, download, and so on). Downloador creates 3 DownloadTask (which defines 3 files for each file to download) to download the specific start and stop position of the file. Here we calculate the start and stop position of each thread through the size of the file. We can refer to "AsyncTask multithread breakpoint continuation" in detail.

1, Downloador class

 package com.bbk.lling.multitaskdownload.downloador; import android.content.Context; import android.content.Intent; import; .text.TextUtils; import android.util.Log; import android.widget.Toast; import com.bbk.lling.multitaskdownload.beans.AppContent; import com.bbk.lling.multitaskdownload.beans.DownloadInfo; import com.bbk.lling.multitaskdownload.db.DownloadFileDAO; import Consultants ODAO; import com.bbk.lling.multitaskdownload.utils.DownloadUtils; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import; Port java.util.concurrent.Executor; import java.util.concurrent.Executors; / * * @Class: Downloador * @Description: task downloader * @author: lling (www.cnblogs.com/liuling) * @Date: 2015/10/13 * / NT THREAD_POOL_SIZE = 9; / / / / / / / thread pool size is 9 private static final int THREAD_NUM = 3; / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / K> tasks; private InnerHandler handler = new InnerHandler (); private AppContent appContent; / / / / / to be downloaded application private long. Text, AppContent appContent) {this.context = context; this.appContent = appContent; this.downloadPath = DownloadUtils.getDownloadPath ();} / * * * / * * begin to download * / public void download () {} {} {} Ow (); return;} if (appContent = = null) {throw new IllegalArgumentException ("download content can not]);} NSE response = null; try {response = client.execute (request); fileLength = response.getEntity ().GetContentLength ();} catch. NloadInfo> lists = DownloadInfoDAO.getInstance (context.getApplicationContext ()).GetDownloadInfosByUrl (appContent.getUrl ()); for (DownloadInfo Info: lists) {downloadLength + = info.getDownloadLength ();} / / insert file downloaded to the database CationContext ()).InsertDownloadFile (appContent); Message.obtain (handler, GET_LENGTH_SUCCESS).SendToTarget ();}}.start ();} / * * / * * begins to create AsyncTask downloading * / private void beginDownload () {} ); long blockLength = fileLength / THREAD_NUM; for (int i = 0; I < THREAD_NUM; i++) {long = = = {/ / / / / / / / / / / / / / / / / / / / / / / / / = / / / / / / / / / / / / / / / / / / / i++) = {/ / / / / / / / / / / / / / / / / = = = = = = = = = = = = {= = = = = = 1). / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / Tasks = new ArrayList< DownloadTask> ();} tasks.add (task);} / * * suspend download * / public void pause () {for (DownloadTask task) {} {} {}))) {} {};}}} {} ; appContent.setStatus (AppContent.Status.PAUSED); DownloadFileDAO.getInstance (context.getApplicationContext ()).UpdateDownloadFile (appContent);} / * * * add the downloaded size to zero * / protected synchronized void resetDownloadLength () {this.downloadLength = 0;} / * * add the downloaded size * Multithreaded access requires locking * / @param size * / protected synchronized void updateDownloadLength (long size) {this.downloadLength + = size; / / / / notifications update interface int percent = = = 100 AdLength = = = = fileLength) {appContent.setDownloadPercent (100); / / / / / above, sometimes there will be a little error, to percent=99 appContent.setStatus (AppContent.Status.FINISHED); DownloadFileDAO.getInstance (context.getApplicationContext ()).UpdateDownloadFile (appContent);} Intent intent = {} Nstants.DOWNLOAD_MSG); if (appContent.getStatus () = = = = AppContent.Status.WAITING) {appContent.setStatus (AppContent.Status.DOWNLOADING);} Bundle bundle = new Bundle (); bundle.putParcelable ("appContent");} GetDownloadPath {() {return downloadPath;} private class InnerHandler extends Handler {@Override public void handleMessage (}) 

 package com.bbk.lling.multitaskdownload.downloador; import android.content.Context; import android.os.AsyncTask; import android.util.Log; import com.bbk.lling.multitaskdownload.beans.DownloadInfo; P.Header; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicHeader; import; NputStream; import java.io.OutputStream; import java.io.RandomAccessFile; import java.net.MalformedURLException; / * * @Class: DownloadTask * @Description: file download AsyncTask * Ng, Integer, Long> {private static final String TAG = "DownloadTask"; private int taskId; Int taskId, long beginPosition, long endPosition, Downloador downloador, Context context) {this.taskId = taskId; } @Override protected void onPreExecute () {Log.e (TAG, "onPreExecute");} @Override protected void onPostExecute (Long) {{} Notification downloador is added to the downloaded size / / / downloador.updateDownloadLength (values[0]);} @Override protected void onCancelled () {Log.e (TAG, "onCancelled"); / / / downloador.updateDownloadInfo (null);} / / here the role of judgment is: If you are still waiting for a pause, run to here already cancel, just exit if (isCancelled ()) {return null;} url = params[0]; if (url = null) {return null;} RandomAccessFile FOS = null; OutputStream output = null; DownloadInfo downloadInfo = null; try {/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / 1) .getDownloadInfoByTaskIdAndUrl (taskId, URL); / / / / / / here, continue downloading / / here to judge file.exists (), judge whether it is deleted by the user, if the file is not downloaded, but has been deleted by the user, then re download if (file.exists () & & downloadInfo! = null) {if (download) Info.isDownloadSuccess () = = = 1) {/ / / download completes return null directly;} beginPosition = beginPosition + downloadInfo.getDownloadLength (); downloadLength = downloadInfo.getDownloadLength ();} if (! File.exists ()) {/ / if this task has been downloaded, but the file is deleted by the user, then it needs to be deleted Reset the download length and download downloador.resetDow again.


This concludes the body part