android async http 断点续传,Android 断点续传下载 通过AsyncTask实现

该博客介绍了一种在Android中使用AsyncTask实现断点续传下载的方法,通过更新进度条并利用SharedPreferences保存下载进度。在主线活动中监听按钮事件,启动和暂停下载任务。当按下返回键时模拟暂停下载,同时详细展示了下载任务的执行过程,包括获取文件长度、建立HTTP连接、读取和写入文件等步骤。
摘要由CSDN通过智能技术生成

Android 断点续传下载

通过AsyncTask执行下载任务,更新进度条,SharedPreferences保存下载进度 ,简单易懂。

package com.xk.myfiledownload.ui;

import java.io.File;

import java.io.IOException;

import java.io.InputStream;

import java.io.RandomAccessFile;

import java.net.HttpURLConnection;

import java.net.URL;

import org.apache.http.HttpStatus;

import android.app.Activity;

import android.content.SharedPreferences;

import android.content.SharedPreferences.Editor;

import android.os.AsyncTask;

import android.os.Bundle;

import android.os.Environment;

import android.util.Log;

import android.view.KeyEvent;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.ProgressBar;

import android.widget.Toast;

import com.xk.myfiledownload.R;

public class MainActivity extends Activity implements OnClickListener {

private Button btnStart;

private Button btnStop;

private ProgressBar pb;

private int length;

private int start;

private boolean isPause;

private SharedPreferences mSharedPreferences;

private Editor mEditor;

public int mFinished;

private boolean flag;

// 下载路径

private final static String MYURL = "http://www.imooc.com/mobile/mukewang.apk";

// 下载地址

private final static String DIRSTR = Environment

.getExternalStorageDirectory().getAbsolutePath() + "/";

// 下载文件名

private final static String FILENAME = "mukewang.apk";

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.mian_layout);

init();

}

/*

* 重写返回键,模拟返回按下暂停键

*/

@Override

public boolean onKeyDown(int keyCode, KeyEvent event) {

switch (keyCode) {

case KeyEvent.KEYCODE_BACK:

btnStop.performClick();// 模拟按下暂停键

break;

default:

break;

}

return super.onKeyDown(keyCode, event);

}

/*

* 初始化控件

*/

private void init() {

btnStart = (Button) findViewById(R.id.button1);

btnStop = (Button) findViewById(R.id.button2);

pb = (ProgressBar) findViewById(R.id.progressBar1);

pb.setMax(100);

btnStart.setOnClickListener(this);

btnStop.setOnClickListener(this);

}

/*

* 异步处理

*/

class MyAsyncTask extends AsyncTask {

@Override

protected String doInBackground(String... params) {

// 获取文件长度

getFileLength(params[0]);

// 执行下载任务

return download(params[0]);

}

@Override

protected void onPostExecute(String result) {

super.onPostExecute(result);

Log.d("result: ", result);

// 返回值设置Toast

Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT)

.show();

}

@Override

protected void onProgressUpdate(Integer... values) {

super.onProgressUpdate(values);

// 更新进度条

pb.setProgress(values[0]);

}

/*

* 获取文件长度

*/

private void getFileLength(String string) {

HttpURLConnection connection = null;

try {

connection = (HttpURLConnection) new URL(string)

.openConnection();

connection.setRequestMethod("GET");

connection.setConnectTimeout(3000);

if (connection.getResponseCode() != HttpStatus.SC_OK) {

return;

}

length = connection.getContentLength();

} catch (Exception e) {

e.printStackTrace();

} finally {

if (connection != null) {

connection.disconnect();

}

}

}

/*

* 下载任务

*/

private String download(String string) {

HttpURLConnection connection = null;

InputStream in = null;

RandomAccessFile raf = null;

try {

// 从SharedPreferences取出DownloadInfo里的下载进度值

mSharedPreferences = getSharedPreferences("DownloadInfo",

MODE_PRIVATE);

mEditor = mSharedPreferences.edit();

start = mSharedPreferences.getInt("Finished", 0);

mFinished = start;

connection = (HttpURLConnection) new URL(string)

.openConnection();

connection.setRequestMethod("GET");

connection.setConnectTimeout(3000);

Log.d("length: ", length + "");

connection.setRequestProperty("Range", "bytes=" + start + "-"

+ length);

if (connection.getResponseCode() != HttpStatus.SC_PARTIAL_CONTENT) {

return "206";

}

in = connection.getInputStream();

File file = new File(DIRSTR, FILENAME);

raf = new RandomAccessFile(file, "rwd");

Log.d("start:", start + "");

raf.seek(start);

byte[] buffer = new byte[1024 * 4];

int len;

while ((len = in.read(buffer)) != -1) {

raf.write(buffer, 0, len);

mFinished += len;

// 设置进度条的值

publishProgress(mFinished * 100 / length);

// 判断是否暂停

if (isPause) {

Log.d("mFinished:", mFinished + "");

// 将进度值存入SharedPreferences中

mEditor.putInt("Finished", mFinished);

mEditor.commit();

if (raf != null) {

raf.close();

}

return "暂停";

}

}

flag = false;

// 下载完成,从SharedPreferences移除下载进度

mEditor.remove("Finished");

mEditor.commit();

return "下载完成!";

} catch (Exception e) {

e.printStackTrace();

return e.getMessage();

} finally {

try {

if (raf != null) {

raf.close();

}

if (in != null) {

in.close();

}

if (connection != null) {

connection.disconnect();

}

} catch (IOException e) {

e.printStackTrace();

return e.getMessage();

}

}

}

}

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.button1:

if (flag) {

return;

}

isPause = false;

// 防止多次点击下载,造成多个下载

flag = true;

Log.d("onClick", "开始");

// 执行下载任务

new MyAsyncTask().execute(MYURL);

break;

case R.id.button2:

// 通过isPause暂停下载

isPause = true;

flag = false;

break;

default:

break;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值