网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

android中的多线程下载怎么利用AsyncTask实现

android中的多线程下载怎么利用AsyncTask实现?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

创新互联主营龙口网站建设的网络公司,主营网站建设方案,APP应用开发,龙口h5微信小程序开发搭建,龙口网站营销推广欢迎龙口等地区企业咨询

01 效果图

android中的多线程下载怎么利用AsyncTask实现

02 核心类 - DownloadTask.class

public class DownloadTask extends AsyncTask {
  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 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();
        }
      }
    }
  }
}

关于android中的多线程下载怎么利用AsyncTask实现问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。


名称栏目:android中的多线程下载怎么利用AsyncTask实现
文章来源:http://bjjierui.cn/article/jgcgde.html

其他资讯