#1. Download the Utils Project
Https://github.com/wyouflf/xUtils
#2布局文件里实现UI
<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" tools:context= ". Mainactivity "> <edittext android:id=" @+id/tv_path " android:layout_width=" Match_parent " android:layout_height= "Wrap_content" android:text= "Http://down.360safe.com/inst.exe"/> <button android:layout_margintop= "10dip" android:layout_width= "match_parent" android:onclick= "Download " android:layout_height=" wrap_content " android:text=" Download " /> <textview android: Id= "@+id/tv_info" android:layout_width= "match_parent" android:layout_margintop= "10dip" android: layout_height= "Wrap_content" android:text= "message" /> </LinearLayout>
#3. Implementing code functionality in Mainactivity
Package Com.wzw.downloaddemo;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.os.bundle;import Android.app.activity;import Android.view.menu;import Android.view.View;import Android.widget.edittext;import Android.widget.textview;public class Mainactivity extends Activity {private EditText Etpath;private TextView tvinfo; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.activity_main); etpath= (EditText) Findviewbyid (R.id.tv_path); tvInfo= (TextView) Findviewbyid (r.id.tv_info);} public void Download (View v) {String path =etpath.gettext (). toString (); Httputils http = new Httputils (); Http.download (Path, "/sdcard/360.exe", True, True, new Requestcallback<file> () {& Lt;span style= "White-space:pre" ></span>@Override public void OnStart () {tvinfo.settext ("Connecting ..."); } @Override public void onloading (long, Long, Boolean isuploading) {Tvinfo.settext (current +) /"+ total); } @Override public void OnFailure (httpexception error, String msg) {tvinfo.settext (msg); } @Overridepublic void onsuccess (responseinfo<file> responseinfo) {//TODO auto-generated method Stubtvinfo.settext ("Downloaded:" + responseInfo.result.getPath ());}});}}
It is more convenient to use utils words than in traditional ways.
Android uses open source project Xutils to implement multi-threaded download files