Android利用Service和AsyncTask做断点下载之下载部分

好几年没有写Android程序了最近在往回捡,通过看一本书看到了Service这块然后就想着按照书上的例子写一个可以断点下载的Demo。

首先列出了用到的三方库,就只用到了okhhtp这个网络操作库,连接如下:
https://github.com/square/okhttp

一、先在build.gradle里添加okhttp库的引用

implementation 'com.squareup.okhttp3:okhttp:3.10.0'

由于我现在用到的开发IDE是Android Studio所以只要在配置文件添加这一行配置代码就可以把okhttp加载进来。

二、写异步下载部分的代码

我们做下载肯定不能在主线程下载否则会造成ANR(Application Not Response),所以我们选择用AsyncTask来实现下载部分功能的包装也就是在异步线程中下载文件。

下面先把源码贴出来再来说重点部分代码

package com.example.downloaddemo;

import android.os.AsyncTask;
import android.os.Environment;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class DownloadTask extends AsyncTask<String,Integer,Integer> {

    public static final int TYPE_SUCCESS = 0;
    public static final int TYPE_FAILED = 1;
    public static final int TYPE_PAUSED = 2;
    public static final int TYPE_CANCELED = 3;

    private boolean isCanceled = false;

    private boolean isPaused = false;

    private int lastProgress;

    DownloadListener mListener;

    public DownloadTask(DownloadListener listener) {
        this.mListener = listener;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        //这里可以写执行任务前的代码,例如显示进度条
    }

    @Override
    protected void onPostExecute(Integer status) {
        super.onPostExecute(status);
        //这里是任务执行完毕后调用,可以做UI操作
        //这里的aBoolean是执行任务后返回的结果可以根据这个值做相应操作
        //执行收尾操作
        switch (status) {
            case TYPE_SUCCESS:
                mListener.onSuccess();
                break;
            case TYPE_FAILED:
                mListener.onFailed();
                break;
            case TYPE_PAUSED:
                mListener.onPaused();
                break;
            case TYPE_CANCELED:
                mListener.onCanceled();
                break;
        }
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        //在这里更新进度
        int progress = values[0];
        if (progress > lastProgress) {
            mListener.onProgress(progress);
            lastProgress = progress;
        }
    }

    @Override
    protected void onCancelled(Integer aInteger) {
        super.onCancelled(aInteger);
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
    }

    @Override
    protected Integer doInBackground(String... params) {
        //这里执行耗时比较长的逻辑,比如下载大文件
        publishProgress(30); //调用这个会触发onProgressUpdate方法触发从而更新进度
        InputStream is = null;
        RandomAccessFile savedFile = null;
        File file = null;
        try {
            long downloadedLength = 0;
            String downloadUrl = params[0];
            String fileName = downloadUrl.substring(downloadUrl.lastIndexOf("/"));
            String directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath();
            file = new File(directory + fileName);
            if (file.exists()) {
                downloadedLength = file.length();
            }
            long contentLength = getContentLength(downloadUrl);
            if (contentLength == 0) {
                return TYPE_FAILED;
            } else if (contentLength == downloadedLength) {
                return  TYPE_SUCCESS;
            }
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder().addHeader("RANGE", "bytes=" + downloadedLength + "-").url(downloadUrl).build();
            Response response = client.newCall(request).execute();
            if (response != null) {
                is = response.body().byteStream();
                savedFile = new RandomAccessFile(file,"rw");
                savedFile.seek(downloadedLength);
                byte[] b = new byte[1024];
                int total = 0;
                int len;
                while ((len = is.read(b)) != -1) {
                    if (isCanceled) {
                        return TYPE_CANCELED;
                    } else if (isPaused) {
                        return TYPE_PAUSED;
                    } else {
                        total += len;
                        savedFile.write(b,0,len);
                        int progress = (int)((total + downloadedLength) * 100 / contentLength);
                        publishProgress(progress);
                    }
                }
                response.body().close();
                return TYPE_SUCCESS;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (is != null) {
                    is.close();
                }

                if (savedFile != null) {
                    savedFile.close();
                }

                if (isCanceled && file != null) {
                    file.delete();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        return TYPE_FAILED;
    }

    public void pauseDownload() {
        isPaused = true;
    }

    public void cancelDownload() {
        isCanceled = true;
    }

    private long getContentLength(String downloadUrl) throws IOException {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(downloadUrl).build();
        Response response = client.newCall(request).execute();
        if (response != null && response.isSuccessful()) {
            long contentLength = response.body().contentLength();
            response.close();
            return contentLength;
        }
        return 0;
    }
}

相信只要写过安卓用过AnsyncTask的同学应该就知道它的子类必须实现的几大方法如下:

  • onPreExecute //在执行任务之前可以做一些操作例如要显示Progress
  • doInBackground //这里做比较繁重的任务,例如下载文件
  • onProgressUpdate //更新进度,通过在doInBackground方法里调用publishProgress实现
  • onPostExecute //整个任务执行结束后的收尾工作在这里做
  • onCancelled //取消任务

1、在doInBackground方法里我们做了下载的功能,首先先找本地下载过的文件并读取大小并获取远程文件总大小计算从哪开始下载或是否下载已经完成了。

long downloadedLength = 0;
            String downloadUrl = params[0];
            String fileName = downloadUrl.substring(downloadUrl.lastIndexOf("/"));
            String directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath();
            file = new File(directory + fileName);
            if (file.exists()) {
                downloadedLength = file.length();
            }
            long contentLength = getContentLength(downloadUrl);
            if (contentLength == 0) {
                return TYPE_FAILED;
            } else if (contentLength == downloadedLength) {
                return  TYPE_SUCCESS;
            }

2、进行前期是否下载完成和下载进度后开始进行下载操作,重要的是下载起始点设置以及持续下载:

while ((len = is.read(b)) != -1) {
                    if (isCanceled) {
                        return TYPE_CANCELED;
                    } else if (isPaused) {
                        return TYPE_PAUSED;
                    } else {
                        total += len;
                        savedFile.write(b,0,len);
                        int progress = (int)((total + downloadedLength) * 100 / contentLength);
                        publishProgress(progress);
                    }
                }

在如上代码持续从网络读取文件并将新下载部分写入(追加写入)文件,这样实现断点下载。
在最后一定要记住做收尾操作防止内存泄露

finally {
            try {
                if (is != null) {
                    is.close();
                }

                if (savedFile != null) {
                    savedFile.close();
                }

                if (isCanceled && file != null) {
                    file.delete();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
0 0 vote
Article Rating
Subscribe
提醒
guest
0 评论
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x