服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android 使用AsyncTask实现断点续传

Android 使用AsyncTask实现断点续传

2022-02-25 15:36残剑_ Android

这篇文章主要介绍了Android 使用AsyncTask实现断点续传的实例代码,非常不错,具有一定的参考借鉴价值,需要的朋友参考下吧

之前公司里面项目的下载模块都是使用xUtils提供的,最近看了下xUtils的源码,它里面也是使用AsyncTask来执行异步任务的,它的下载也包含了断点续传的功能。这里我自己也使用AsyncTask也实现了简单的断点续传的功能。

  首先说一说AsyncTask吧,先来看看AsyncTask的定义:

?
1
public abstract class AsyncTask<Params, Progress, Result>

  三种泛型类型分别代表“启动任务执行的输入参数”、“后台任务执行的进度”、“后台计算结果的类型”。在特定场合下,并不是所有类型都被使用,如果没有被使用,可以用java.lang.Void类型代替。

  一个异步任务的执行一般包括以下几个步骤:

  1.execute(Params... params),执行一个异步任务,需要我们在代码中调用此方法,触发异步任务的执行。

  2.onPreExecute(),在execute(Params... params)被调用后立即执行,一般用来在执行后台任务前对UI做一些标记。

  3.doInBackground(Params... params),在onPreExecute()完成后立即执行,用于执行较为费时的操作,此方法将接收输入参数和返回计算结果。在执行过程中可以调用publishProgress(Progress... values)来更新进度信息。

  4.onProgressUpdate(Progress... values),在调用publishProgress(Progress... values)时,此方法被执行,直接将进度信息更新到UI组件上。

  5.onPostExecute(Result result),当后台操作结束时,此方法将会被调用,计算结果将做为参数传递到此方法中,直接将结果显示到UI组件上。

  在使用的时候,有几点需要格外注意:

  1.异步任务的实例必须在UI线程中创建。

  2.execute(Params... params)方法必须在UI线程中调用。

  3.不要手动调用onPreExecute(),doInBackground(Params... params),onProgressUpdate(Progress... values),onPostExecute(Result result)这几个方法。

  4.不能在doInBackground(Params... params)中更改UI组件的信息。

  5.一个任务实例只能执行一次,如果执行第二次将会抛出异常。

  下面是使用AsyncTask实现断点续传的代码:

  断点续传的思路其实也挺简单,首先判断待下载的文件在本地是否存在,如果存在,则表示该文件已经下载过一部分了,只需要获取文件当前大小即已下载大小,设置给http的header就行了:

?
1
2
Header header_size = new BasicHeader("Range", "bytes=" + readedSize + "-");
request.addHeader(header_size);

  1、布局文件代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<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:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".AsyncTaskDemoActivity"
  android:orientation="vertical">
  <Button
    android:id="@+id/begin"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="开始下载"/>
  <Button
    android:id="@+id/end"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="暂停下载"/>
  <ProgressBar
    android:id="@+id/progressbar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="fill_parent"
    android:layout_height="3dp"
    android:layout_marginTop="10dp"
    android:max="100" />
</LinearLayout>

布局比较简单,就两个按钮和一个进度条控件,按钮控制下载与暂停。

  2、断点续传核心代码:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package com.bbk.lling.myapplication;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;
import org.apache.http.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 java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.net.MalformedURLException;
public class AsyncTaskDemoActivity extends Activity {
  private ProgressBar progressBar;
  //下载路径
  private String downloadPath = Environment.getExternalStorageDirectory() +
      File.separator + "download";
  private DownloadAsyncTask downloadTask;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_async_task_demo);
    progressBar = (ProgressBar) findViewById(R.id.progressbar);
    //开始下载
    findViewById(R.id.begin).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        /**
         * 一个AsyncTask只能被执行一次,否则会抛异常
         * java.lang.IllegalStateException: Cannot execute task: the task is already running.
         * 如果要重新开始任务的话要重新创建AsyncTask对象
         */
        if(downloadTask != null && !downloadTask.isCancelled()) {
          return;
        }
        downloadTask = new DownloadAsyncTask("http://bbk-lewen.u.qiniudn.com/3d5b1a2c-4986-4e4a-a626-b504a36e600a.flv");
        downloadTask.execute();
      }
    });
    //暂停下载
    findViewById(R.id.end).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        if(downloadTask != null && downloadTask.getStatus() == AsyncTask.Status.RUNNING) {
          downloadTask.cancel(true);
        }
      }
    });
  }
  /**
   * 下载的AsyncTask
   */
  private class DownloadAsyncTask extends AsyncTask<String, Integer, Long> {
    private static final String TAG = "DownloadAsyncTask";
    private String mUrl;
    public DownloadAsyncTask(String url) {
      this.mUrl = url;
    }
    @Override
    protected Long doInBackground(String... params) {
      Log.i(TAG, "downloading");
      if(mUrl == null) {
        return null;
      }
      HttpClient client = new DefaultHttpClient();
      HttpGet request = new HttpGet(mUrl);
      HttpResponse response = null;
      InputStream is = null;
      RandomAccessFile fos = null;
      OutputStream output = null;
      try {
        //创建存储文件夹
        File dir = new File(downloadPath);
        if(!dir.exists()) {
          dir.mkdir();
        }
        //本地文件
        File file = new File(downloadPath + File.separator + mUrl.substring(mUrl.lastIndexOf("/") + 1));
        if(!file.exists()){
          //创建文件输出流
          output = new FileOutputStream(file);
          //获取下载输入流
          response = client.execute(request);
          is = response.getEntity().getContent();
          //写入本地
          file.createNewFile();
          byte buffer [] = new byte[1024];
          int inputSize = -1;
          //获取文件总大小,用于计算进度
          long total = response.getEntity().getContentLength();
          int count = 0; //已下载大小
          while((inputSize = is.read(buffer)) != -1) {
            output.write(buffer, 0, inputSize);
            count += inputSize;
            //更新进度
            this.publishProgress((int) ((count / (float) total) * 100));
            //一旦任务被取消则退出循环,否则一直执行,直到结束
            if(isCancelled()) {
              output.flush();
              return null;
            }
          }
          output.flush();
        } else {
          long readedSize = file.length(); //文件大小,即已下载大小
          //设置下载的数据位置XX字节到XX字节
          Header header_size = new BasicHeader("Range", "bytes=" + readedSize + "-");
          request.addHeader(header_size);
          //执行请求获取下载输入流
          response = client.execute(request);
          is = response.getEntity().getContent();
          //文件总大小=已下载大小+未下载大小
          long total = readedSize + response.getEntity().getContentLength();
          //创建文件输出流
          fos = new RandomAccessFile(file, "rw");
          //从文件的size以后的位置开始写入,其实也不用,直接往后写就可以。有时候多线程下载需要用
          fos.seek(readedSize);
          //这里用RandomAccessFile和FileOutputStream都可以,只是使用FileOutputStream的时候要传入第二哥参数true,表示从后面填充
//          output = new FileOutputStream(file, true);
          byte buffer [] = new byte[1024];
          int inputSize = -1;
          int count = (int)readedSize;
          while((inputSize = is.read(buffer)) != -1) {
            fos.write(buffer, 0, inputSize);
//            output.write(buffer, 0, inputSize);
            count += inputSize;
            this.publishProgress((int) ((count / (float) total) * 100));
            if(isCancelled()) {
//              output.flush();
              return null;
            }
          }
//          output.flush();
        }
      } catch (MalformedURLException e) {
        Log.e(TAG, e.getMessage());
      } catch (IOException e) {
        Log.e(TAG, e.getMessage());
      } finally{
        try{
          if(is != null) {
            is.close();
          }
          if(output != null) {
            output.close();
          }
          if(fos != null) {
            fos.close();
          }
        } catch(Exception e) {
          e.printStackTrace();
        }
      }
      return null;
    }
    @Override
    protected void onPreExecute() {
      Log.i(TAG, "download begin ");
      Toast.makeText(AsyncTaskDemoActivity.this, "开始下载", Toast.LENGTH_SHORT).show();
      super.onPreExecute();
    }
    @Override
    protected void onProgressUpdate(Integer... values) {
      super.onProgressUpdate(values);
      Log.i(TAG, "downloading " + values[0]);
      //更新界面进度条
      progressBar.setProgress(values[0]);
    }
    @Override
    protected void onPostExecute(Long aLong) {
      Log.i(TAG, "download success " + aLong);
      Toast.makeText(AsyncTaskDemoActivity.this, "下载结束", Toast.LENGTH_SHORT).show();
      super.onPostExecute(aLong);
    }
  }
}

这样简单的断点续传功能就实现了,这里只是单个线程的,如果涉及多个线程同时下载,那复杂很多,后面再琢磨琢磨。

源码下载:https://github.com/liuling07/MultiTaskAndThreadDownload

总结

以上所述是小编给大家介绍的Android 使用AsyncTask实现断点续传,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:http://www.cnblogs.com/liuling/p/2015-10-10-01.html

延伸 · 阅读

精彩推荐