标签:提示 服务 方式 文件名 etc 断点 horizon 布局 mat
1、DbUtils模块
2、ViewUtils模块
3、HttpUtils模块:
4、BitmapUtils模块
布局文件:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:id="@+id/activity_main" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 android:paddingBottom="@dimen/activity_vertical_margin" 8 android:paddingLeft="@dimen/activity_horizontal_margin" 9 android:paddingRight="@dimen/activity_horizontal_margin" 10 android:paddingTop="@dimen/activity_vertical_margin" 11 tools:context="com.ahu.lichang.httputils_multithreaddownload.MainActivity" 12 android:orientation="vertical"> 13 <Button 14 android:text="HttpUtils多线程下载" 15 android:onClick="download" 16 android:layout_width="wrap_content" 17 android:layout_height="wrap_content" /> 18 <TextView 19 android:id="@+id/tv_failure" 20 android:layout_width="wrap_content" 21 android:layout_height="wrap_content" 22 android:hint="下载失败提示" /> 23 <ProgressBar 24 android:id="@+id/pb" 25 style="@android:style/Widget.ProgressBar.Horizontal" 26 android:layout_width="match_parent" 27 android:layout_height="wrap_content" /> 28 <TextView 29 android:id="@+id/tv_progress" 30 android:text="下载进度" 31 android:layout_width="wrap_content" 32 android:layout_height="wrap_content" /> 33 </LinearLayout>
MainActivity:
1 package com.ahu.lichang.httputils_multithreaddownload; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.View; 6 import android.widget.ProgressBar; 7 import android.widget.TextView; 8 import android.widget.Toast; 9 10 import com.lidroid.xutils.HttpUtils; 11 import com.lidroid.xutils.exception.HttpException; 12 import com.lidroid.xutils.http.ResponseInfo; 13 import com.lidroid.xutils.http.callback.RequestCallBack; 14 15 import java.io.File; 16 17 public class MainActivity extends Activity { 18 private TextView tv_failure; 19 private TextView tv_progress; 20 private ProgressBar pb; 21 @Override 22 protected void onCreate(Bundle savedInstanceState) { 23 super.onCreate(savedInstanceState); 24 setContentView(R.layout.activity_main); 25 tv_failure = (TextView) findViewById(R.id.tv_failure); 26 tv_progress = (TextView) findViewById(R.id.tv_progress); 27 pb = (ProgressBar) findViewById(R.id.pb); 28 } 29 30 public void download(View view){ 31 String path = "http://172.23.13.179:8080/QQPlayer.exe"; 32 HttpUtils httpUtils = new HttpUtils(); 33 httpUtils.download(path,//下载地址 34 "storage/sdcard/QQPlayer.exe",//下载的数据保存的路径和文件名 35 true,//是否开启断点续传 36 true,//如果服务器响应头中包含了文件名,那么下载完毕后自动重命名 37 new RequestCallBack<File>() {//侦听下载状态 38 @Override 39 public void onSuccess(ResponseInfo<File> responseInfo) { 40 Toast.makeText(MainActivity.this,responseInfo.result.getPath(),Toast.LENGTH_SHORT).show(); 41 } 42 43 @Override 44 public void onFailure(HttpException e, String s) { 45 tv_failure.setText(s); 46 } 47 48 @Override 49 public void onLoading(long total, long current, boolean isUploading) { 50 super.onLoading(total, current, isUploading); 51 pb.setMax((int) total); 52 pb.setProgress((int) current); 53 tv_progress.setText(current * 100 / total + "%"); 54 } 55 } 56 ); 57 } 58 }
权限:
1 <!--XUtils要添加两个权限--> 2 <uses-permission android:name="android.permission.INTERNET"/> 3 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
运行结果:
XUtils开源框架的使用(HttpUtils支持多线程断点续传)
标签:提示 服务 方式 文件名 etc 断点 horizon 布局 mat
原文地址:http://www.cnblogs.com/ahu-lichang/p/6602279.html