一。权限
1 2 3 4 |
<uses-permission android:name="android.permission.INTERNET"/> <!-- 访问网络 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <!-- 写内存卡 --> |
二。布局
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
<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" > <TextView android:id="@+id/tv_uri" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="https://github.com/wyouflf/xUtils/archive/master.zip" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="doDownload" android:text="下载" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="doStopDownload" android:text="停止" /> </LinearLayout> <TextView android:id="@+id/tv_info" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="信息----" /> </LinearLayout> |
三。主程
1.XUtils准备
直接在src里放入com源码包,或者在libs里放入jar包并加入编译路径。
2.主程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
/** * 原名 * https://github.com/yangfuhai/afinal * * 达文西,要你命3000 * https://github.com/wyouflf/xUtils * https://github.com/wyouflf/xUtils/archive/master.zip */ public class MainActivity extends ActionBarActivity { private TextView tv_uri; // 文件的uri private TextView tv_info; // 提示 private HttpHandler handler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_uri = (TextView) findViewById(R.id.tv_uri); tv_info = (TextView) findViewById(R.id.tv_info); } public void doDownload(View v){ String uri = tv_uri.getText().toString(); if(TextUtils.isEmpty(uri)){ Toast.makeText(MainActivity.this, "输入文件的下载地址", 0); return; }else{ Toast.makeText(MainActivity.this, "准备开始下载...", 0); // 1.创建下载器 HttpUtils http = new HttpUtils(); // 2.最大开启线程数量 http.configRequestThreadPoolSize(4); handler = http.download( uri, // 1)源文件地址 "/mnt/sdcard/Download/xUtils.zip", // 2)下载保存地址 true, // 3)如果目标文件存在,接着未完成的部分继续下载。服务器不支持RANGE时将从新下载。 true, // 4)如果从请求返回信息中获取到文件名,下载完成后自动重命名。 new RequestCallBack<File>() { // 5)回调函数,处理整个下载事件 // (1)事件:开始下载 public void onStart() { // 交由主线程处理 tv_info.setText("conn..."); } // (2)事件:下载中 public void onLoading(long total, long current, boolean isUploading) { tv_info.setText(current + "/" + total); } // (3)事件:下载成功 public void onSuccess(ResponseInfo<File> responseInfo) { tv_info.setText("downloaded:" + responseInfo.result.getPath()); } // (4)事件:下载失败 public void onFailure( com.lidroid.xutils.exception.HttpException error, String msg) { tv_info.setText(msg); } }); } } public void doStopDownload(View v){ //调用cancel()方法停止下载 handler.cancel(); tv_info.setText("停止下载-----"); } } |
声明
本文由崔维友 威格灵 cuiweiyou vigiles cuiweiyou 原创,转载请注明出处:http://www.gaohaiyan.com/203.html
承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设