1. Programmers can also implement multi-threaded download, but the code is relatively large, and, there are many details need to consider, on GitHub, someone write good code, we can take it down to use, Save our development program time
2. Guide Package: Xutils-2.6.14.jar, file can go to https://github.com/wyouflf/xUtils download
3.fragment_main.xml
<LinearLayoutxmlns: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" > <ButtonAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Start Download"Android:onclick= "click" /> <ProgressBarAndroid:id= "@+id/pb_bar"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"style= "@android: Style/widget.progressbar.horizontal" /> <TextViewAndroid:id= "@+id/tv_progress"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:textcolor= "#A52A2A"android:textsize= "25SP" /> <TextViewAndroid:id= "@+id/tv_failure"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:textcolor= "#FF6347" /></LinearLayout>
4.mainactivity.java
PackageCom.example.xutilsmultithreaddownload;ImportJava.io.File;Importcom.lidroid.xutils.HttpUtils;Importcom.lidroid.xutils.exception.HttpException;ImportCom.lidroid.xutils.http.HttpHandler;ImportCom.lidroid.xutils.http.ResponseInfo;ImportCom.lidroid.xutils.http.callback.RequestCallBack;Importandroid.app.Activity;ImportAndroid.app.ActionBar;Importandroid.app.Fragment;ImportAndroid.os.Bundle;Importandroid.os.Environment;ImportAndroid.view.LayoutInflater;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.ProgressBar;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast;ImportAndroid.os.Build; Public classMainactivityextendsActivity {//text of the error prompt PrivateTextView tv_failure; //progress bar PrivateProgressBar Pb_bar; //text for Progress display PrivateTextView tv_progress; @Overrideprotected voidonCreate (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 voidClick (View v) {//file nameString fileName = "Esurfing_v2.1.exe"; //PathString Path = "http://192.168.1.66:8080/" +FileName; Httputils http=Newhttputils (); HttpHandler Handler= Http.download (path, environment.getexternalstoragedirectory () + "/" + FileName,true,true,NewRequestcallback<file>() { //call after successful download@Override Public voidOnsuccess (responseinfo<file>arg0) { //TODO auto-generated Method StubToast.maketext (mainactivity. This, Arg0.result.getPath (), 0). Show (); } //Download failed call@Override Public voidonfailure (HttpException arg0, String arg1) {//TODO auto-generated Method StubTv_failure.settext (ARG1); } //when downloading, call@Override Public voidOnloading (LongTotalLongCurrentBooleanisuploading) { //TODO auto-generated Method Stub Super. onloading (Total, Current, isuploading); //set the maximum value of the progress barPb_bar.setmax ((int) total); //set current progress for ProgressPb_bar.setprogress ((int) (current); //text shows the current progressTv_progress.settext (current*100/total+ "%"); } }); }}
5. Permission to join
<android:name= "Android.permission.INTERNET"/>< android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
6.xUtils use
7. Operation Effect
Android uses open source xutils for multi-threaded downloads (not original)