android使用AsyncTask实现多线程下载实例

发布时间 - 2026-01-10 22:54:54    点击率:

AsyncTask不仅方便我们在子线程中对UI进行更新操作,还可以借助其本身的线程池来实现多线程任务。下面是一个使用AsyncTask来实现的多线程下载例子。

01 效果图

02 核心类 - DownloadTask.class

public class DownloadTask extends AsyncTask<String, Integer, Integer> {
  public static final int TYPE_SUCCESS = 0;
  public static final int TYPE_FAILURE = 1;
  public static final int TYPE_PAUSE = 2;
  public static final int TYPE_CANCEL = 3;

  public int positionDownload;

  private boolean isPaused = false;
  private boolean isCancelled = false;

  private DownloadListener downloadListener;
  private int lastProgress;

  public DownloadTask(DownloadListener downloadListener){
    this.downloadListener = downloadListener;
  }

  public void setDownloadListener(DownloadListener downloadListener){
    this.downloadListener = downloadListener;
  }

  @Override
  protected Integer doInBackground(String... params) {
    InputStream is = null;
    RandomAccessFile savedFile = null;
    File file = null;

    long downloadLength = 0;
    String downloadUrl = params[0];
    positionDownload = Integer.parseInt(params[1]);
    String fileName = downloadUrl.substring(downloadUrl.lastIndexOf("/"));
    String directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath();
    file = new File(directory + fileName);

    if(file.exists()){
      downloadLength = file.length();
    }

    long contentLength = getContentLength(downloadUrl);
    if(contentLength == 0){
      return TYPE_FAILURE;
    } else if(contentLength == downloadLength){
      return TYPE_SUCCESS;
    }

    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder()
                  .addHeader("RANGE", "bytes="+downloadLength+"-")
                  .url(downloadUrl)
                  .build();
    try {
      Response response = client.newCall(request).execute();
      if(response != null){
        is = response.body().byteStream();
        savedFile = new RandomAccessFile(file, "rw");
        savedFile.seek(downloadLength);
        byte[] buffer = new byte[1024];
        int total = 0;
        int length;

        while((length = is.read(buffer)) != -1){
          if(isCancelled){
            response.body().close();
            return TYPE_CANCEL;
          } else if(isPaused) {
            response.body().close();
            return TYPE_PAUSE;
          }

          total += length;
          savedFile.write(buffer, 0, length);

          int progress = (int) ((total + downloadLength) * 100 / contentLength);
          int currentDownload = (int) (total + downloadLength);
          publishProgress(positionDownload, progress, currentDownload, (int) contentLength);
        }

        response.body().close();
        return TYPE_SUCCESS;
      }
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        if(is != null) is.close();
        if(savedFile != null) savedFile.close();
        if(isCancelled && file != null) file.delete();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }

    return TYPE_FAILURE;
  }

  @Override
  protected void onProgressUpdate(Integer... values) {
    int progress = values[1];
    if(progress > lastProgress){
      downloadListener.onProgress(values[0], progress, values[2], values[3]);
      lastProgress = progress;
    }
  }

  @Override
  protected void onPostExecute(Integer status) {
    switch (status){
      case TYPE_SUCCESS:
        downloadListener.onSuccess(positionDownload);
        break;
      case TYPE_FAILURE:
        downloadListener.onFailure();
        break;
      case TYPE_PAUSE:
        downloadListener.onPause();
        break;
      case TYPE_CANCEL:
        downloadListener.onCancel();
        break;
    }
  }

  public void pauseDownload(){
    isPaused = true;
  }

  public void cancelDownload(){
    isCancelled = true;
  }

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

    return 0;
  }
}

03 核心类 - DownloadService.class

public class DownloadService extends Service {
  private Map<String, DownloadTask> downloadTaskMap = new HashMap<>();

  private DownloadBinder mBinder = new DownloadBinder();

  @Override
  public IBinder onBind(Intent intent) {
    return mBinder;
  }

  private Notification getNotification(String title, int progress) {
    Intent intent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setSmallIcon(R.mipmap.ic_launcher);
    builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
    builder.setContentIntent(pendingIntent);
    builder.setContentTitle(title);
    if(progress > 0){
      builder.setContentText(progress + "%");
      builder.setProgress(100, progress, false);
    }


    return builder.build();
  }

  private NotificationManager getNotificationManager() {
    return (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  }


  class DownloadBinder extends Binder {
    public void startDownload(String url, int position, DownloadListener listener){
      if(!downloadTaskMap.containsKey(url)){
        DownloadTask downloadTask = new DownloadTask(listener);
        downloadTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, url, position+"");
        downloadTaskMap.put(url, downloadTask);
        if(downloadTaskMap.size() == 1){
          startForeground(1, getNotification("正在下载" + downloadTaskMap.size(), -1));
        } else{
          getNotificationManager().notify(1, getNotification("正在下载" + downloadTaskMap.size(), -1));
        }
      }
    }

    public void updateDownload(String url, DownloadListener listener){
      if(downloadTaskMap.containsKey(url)){
        DownloadTask downloadTask = downloadTaskMap.get(url);
        if(downloadTask != null){
          downloadTask.setDownloadListener(listener);
        }
      }

    }

    public void pauseDownload(String url){
      if(downloadTaskMap.containsKey(url)){
        DownloadTask downloadTask = downloadTaskMap.get(url);
        if(downloadTask != null){
          downloadTask.pauseDownload();
        }

        downloadTaskMap.remove(url);

        if(downloadTaskMap.size() > 0){
          getNotificationManager().notify(1, getNotification("正在下载" + downloadTaskMap.size(), -1));
        } else {
          stopForeground(true);
          getNotificationManager().notify(1, getNotification("全部暂停下载", -1));
        }
      }
    }

    public void downloadSuccess(String url){
      if(downloadTaskMap.containsKey(url)){
        DownloadTask downloadTask = downloadTaskMap.get(url);
        downloadTaskMap.remove(url);
        if(downloadTask != null){
          downloadTask = null;
        }

        if(downloadTaskMap.size() > 0){
          getNotificationManager().notify(1, getNotification("正在下载" + downloadTaskMap.size(), -1));
        } else {
          stopForeground(true);
          getNotificationManager().notify(1, getNotification("下载成功", -1));
        }

      }
    }

    public boolean isDownloading(String url){
      if(downloadTaskMap.containsKey(url)){
        return true;
      }

      return false;
    }

    public void cancelDownload(String url){
      if(downloadTaskMap.containsKey(url)){
        DownloadTask downloadTask = downloadTaskMap.get(url);
        if(downloadTask != null){
          downloadTask.cancelDownload();
        }
        downloadTaskMap.remove(url);

        if(downloadTaskMap.size() > 0){
          getNotificationManager().notify(1, getNotification("正在下载" + downloadTaskMap.size(), -1));
        } else {
          stopForeground(true);
          getNotificationManager().notify(1, getNotification("全部取消下载", -1));
        }
      }

      if(url != null){
        String fileName = url.substring(url.lastIndexOf("/"));
        String directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath();
        File file = new File(directory + fileName);

        if(file.exists()){
          file.delete();
          Toast.makeText(DownloadService.this, "Deleted", Toast.LENGTH_SHORT).show();
        }
      }
    }
  }
}

04 源码

下载地址

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


# android  # AsyncTask  # 多线程下载  # asynctask多线程  # Android 使用AsyncTask实现多任务多线程断点续传下载  # Android多线程AsyncTask详解  # Android使用AsyncTask实现多线程下载的方法  # Android开发笔记之:深入理解多线程AsyncTask  # Android 使用AsyncTask实现断点续传  # Android 使用AsyncTask实现多线程断点续传  # 来实现  # 多线程  # 是一个  # 还可以  # 下载地址  # 中对  # 大家多多  # getPath  # exists  # DIRECTORY_DOWNLOADS  # Environment  # getExternalStoragePublicDirectory  # OkHttpClient  # return  # getContentLength  # length  # contentLength  # downloadLength  # downloadUrl  # long 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: 香港服务器部署网站为何提示未备案?  Laravel如何使用Socialite实现第三方登录?(微信/GitHub示例)  Laravel如何将应用部署到生产服务器_Laravel生产环境部署流程  BootStrap整体框架之基础布局组件  HTML5空格和margin有啥区别_空格与外边距的使用场景【说明】  如何用VPS主机快速搭建个人网站?  Python自然语言搜索引擎项目教程_倒排索引查询优化案例  Firefox Developer Edition开发者版本入口  JavaScript实现Fly Bird小游戏  香港服务器网站卡顿?如何解决网络延迟与负载问题?  Laravel如何实现一对一模型关联?(Eloquent示例)  Laravel Eloquent访问器与修改器是什么_Laravel Accessors & Mutators数据处理技巧  焦点电影公司作品,电影焦点结局是什么?  Laravel Seeder填充数据教程_Laravel模型工厂Factory使用  iOS验证手机号的正则表达式  HTML5空格和nbsp有啥关系_nbsp的作用及使用场景【说明】  如何在自有机房高效搭建专业网站?  javascript中数组(Array)对象和字符串(String)对象的常用方法总结  Laravel如何使用Passport实现OAuth2?(完整配置步骤)  魔方云NAT建站如何实现端口转发?  Laravel 419 page expired怎么解决_Laravel CSRF令牌过期处理  如何在IIS中新建站点并配置端口与IP地址?  武汉网站设计制作公司,武汉有哪些比较大的同城网站或论坛,就是里面都是武汉人的?  php json中文编码为null的解决办法  Win11应用商店下载慢怎么办 Win11更改DNS提速下载【修复】  nodejs redis 发布订阅机制封装实现方法及实例代码  网站制作免费,什么网站能看正片电影?  Laravel distinct去重查询_Laravel Eloquent去重方法  猎豹浏览器开发者工具怎么打开 猎豹浏览器F12调试工具使用【前端必备】  宙斯浏览器文件分类查看教程 快速筛选视频文档与图片方法  魔毅自助建站系统:模板定制与SEO优化一键生成指南  如何用低价快速搭建高质量网站?  Laravel如何处理异常和错误?(Handler示例)  js实现获取鼠标当前的位置  济南网站建设制作公司,室内设计网站一般都有哪些功能?  Laravel如何实现数据导出到CSV文件_Laravel原生流式输出大数据量CSV【方案】  如何获取PHP WAP自助建站系统源码?  如何在景安云服务器上绑定域名并配置虚拟主机?  php做exe能调用系统命令吗_执行cmd指令实现方式【详解】  lovemo网页版地址 lovemo官网手机登录  Laravel如何配置和使用缓存?(Redis代码示例)  如何用已有域名快速搭建网站?  如何在云主机上快速搭建多站点网站?  laravel怎么通过契约(Contracts)编程_laravel契约(Contracts)编程方法  北京网页设计制作网站有哪些,继续教育自动播放怎么设置?  UC浏览器如何切换小说阅读源_UC浏览器阅读源切换【方法】  Laravel如何使用Contracts(契约)进行编程_Laravel契约接口与依赖反转  美食网站链接制作教程视频,哪个教做美食的网站比较专业点?  如何在万网主机上快速搭建网站?  如何确保西部建站助手FTP传输的安全性?