在前面的博客中分別介紹了在Java中實現多線程下載和斷點續傳以及在android中實現多線程下載和斷點續傳,這篇博客將介紹使用第三方框架xUtils實現多線程下載和斷點續傳
首先看一下實現的效果,點擊下載文件按鈕後開始下載文件
下載完成後會將下載好的文件保存在SD卡中
實現方式
第一步:使用Android Studio創建一個Android工程
第二步:到http://download.csdn.net/detail/u010105970/9499116連接下下載xUtils框架,下載完成後會會獲得一個壓縮包
第三步:解壓壓縮包,將xUtils-master文件夾下的xUtils-2.6.14.jar拷貝到Android工程所在的目錄下的libs文件夾中
第四步:在Android Studio中導入到libs目錄下的xUtils-2.6.14.jar包,單擊鼠標右鍵,並且選擇Add libarary
第五步:修改activity_main.xml文件
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下載"
android:onClick="click" />
<TextView
android:id="@+id/tv_failure"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ProgressBar
android:id="@+id/pb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@android:style/Widget.ProgressBar.Horizontal" />
<TextView
android:id="@+id/tv_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
第六步:修改MainActivity.java文件
package com.fyt.xutilsdemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import java.io.File;
public class MainActivity extends Activity {
//用於顯示下載失敗時的提示信息
private TextView tv_failure;
//用於顯示下載進度
private TextView tv_progress;
//用於設置進度條中的下載進度
private ProgressBar pb;
//用於記錄下載文件的文件名
private String fileName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//獲得佈局文件上的控件
tv_failure = (TextView) findViewById(R.id.tv_failure);
tv_progress = (TextView) findViewById(R.id.tv_progress);
pb = (ProgressBar) findViewById(R.id.pb);
//設置下載文件的文件名
fileName = "python-2.7.5.amd64.msi";
}
//下載文件按鈕響應函數
public void click(View view) {
//創建HttpUtils對象
HttpUtils utils = new HttpUtils();
//設置下載地址
String path = "http://192.168.0.101:8080/app/" + fileName;
/**
* 下載文件
* 第一個參數:文件的下載地址
* 第二個參數:文件的保存路徑
* 第三個參數:是否支持斷點續傳
* 第四個參數:是否自動重命名
* 第五個參數:下載文件的響應函數
*/
utils.download(
path,
"sdcard/python-2.7.5.amd64.msi",
true,
true,
new RequestCallBack<File>() {
//下載成功後調用此方法
@Override
public void onSuccess(ResponseInfo<File> responseInfo) {
Toast.makeText(
MainActivity.this,
responseInfo.result.getPath(),
Toast.LENGTH_SHORT).show();
}
//下載失敗後調用此方法
@Override
public void onFailure(HttpException e, String s) {
//使用TextView顯示下載失敗的提示信息
tv_failure.setText(s);
}
//設置下載的進度
@Override
public void onLoading(long total, long current, boolean isUploading) {
super.onLoading(total, current, isUploading);
//設置進度條的總進度
pb.setMax((int) total);
//在進度條中設置當前的下載進度
pb.setProgress((int) current);
//在TextView中設置當前的下載進度
tv_progress.setText(current * 100 / total + "%");
}
}
);
}
}
第七步:在配置文件中添加對SD卡的寫權限和訪問網絡權限
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>