Android 使用开源xUtils来实现多线程下载(非原创)
1.程序员自己也是可以实现多线程下载的,只是代码量比较大,而且,其中有许多细节需要考虑到,在GitHub上有人写好的代码,我们可以拿过来使用下,节省了我们开发程序的时间
2.导包:xUtils-2.6.14.jar,文件可以去https://github.com/wyouflf/xUtils下载
3.fragment_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:orientation="vertical" > <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="开始下载" android:onClick="click" /> <ProgressBar android:id="@+id/pb_bar" 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" android:textColor="#A52A2A" android:textSize="25sp" /> <TextView android:id="@+id/tv_failure" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#FF6347" /> </LinearLayout>
4.MainActivity.java
package com.example.xutilsmultithreaddownload; import java.io.File; import com.lidroid.xutils.HttpUtils; import com.lidroid.xutils.exception.HttpException; import com.lidroid.xutils.http.HttpHandler; import com.lidroid.xutils.http.ResponseInfo; import com.lidroid.xutils.http.callback.RequestCallBack; import android.app.Activity; import android.app.ActionBar; import android.app.Fragment; import android.os.Bundle; import android.os.Environment; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; import android.os.Build; public class MainActivity extends Activity { //错误提示的文本 private TextView tv_failure; //进度条 private ProgressBar pb_bar; //进度显示的文本 private TextView tv_progress; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_main); tv_failure = (TextView) findViewById(R.id.tv_failure); pb_bar = (ProgressBar) findViewById(R.id.pb_bar); tv_progress = (TextView) findViewById(R.id.tv_progress); } public void click(View v) { // 文件名字 String fileName = "ESurfing_V2.1.exe"; // 路径 String path = "http://192.168.1.66:8080/" + fileName; HttpUtils http = new HttpUtils(); HttpHandler handler = http.download(path, Environment.getExternalStorageDirectory() + "/" + fileName, true, true, new RequestCallBack<File>() { //下载成功后调用 @Override public void onSuccess(ResponseInfo<File> arg0) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, arg0.result.getPath(), 0).show(); } //下载失败调用 @Override public void onFailure(HttpException arg0, String arg1) { // TODO Auto-generated method stub tv_failure.setText(arg1); } //下载的时候调用 @Override public void onLoading(long total, long current, boolean isUploading) { // TODO Auto-generated method stub super.onLoading(total, current, isUploading); //设置进度条的最大值 pb_bar.setMax((int)total); //设置进度当前的进度 pb_bar.setProgress((int)current); //文本显示当前的进度 tv_progress.setText(current*100/total+"%"); } }); } }
5.加入权限
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
6.xUtils使用
7.运行效果