对于“断电续传”,在任何开发中都显得很重要。xutils对此封装的很好了,可以很简单的实现很多下载功能,其中就包括“断点续传”


主要代码如下:

package com.itydl.xutils;import java.io.File;import com.lidroid.xutils.HttpUtils;import com.lidroid.xutils.exception.HttpException;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.ProgressBar;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity {private TextView tv_failure;private TextView tv_progress;private ProgressBar pb;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);tv_failure = (TextView) findViewById(R.id.tv_failure);tv_progress = (TextView) findViewById(R.id.tv_progress);pb = (ProgressBar) findViewById(R.id.pb);}public void click(View v){HttpUtils utils = new HttpUtils();String fileName = "QQPlayer.exe";//确定下载地址String path = "http://192.168.1.104:8080/" + fileName;utils.download(path, //下载地址"sdcard/QQPlayer.exe", //文件保存路径true,//是否支持断点续传true, new RequestCallBack<File>() {//下载成功后调用@Overridepublic void onSuccess(ResponseInfo<File> arg0) {Toast.makeText(MainActivity.this, arg0.result.getPath(), 0).show();}//下载失败调用@Overridepublic void onFailure(HttpException arg0, String arg1) {// TODO Auto-generated method stubtv_failure.setText(arg1);}                                     //进度条显示下载进度,而且即使断网,点击下载按钮仍然在原来位置下载@Overridepublic void onLoading(long total, long current,boolean isUploading) {// TODO Auto-generated method stubsuper.onLoading(total, current, isUploading);pb.setMax((int)total);pb.setProgress((int)current);tv_progress.setText(current * 100 / total + "%");}});}}


更多相关文章

  1. 【Android】删除已知路径的文件或文件夹
  2. Android按钮设置文字变色
  3. 在Android中单独编译linux kernel驱动模块
  4. Android学习07-----事件处理(2)单选按钮与下拉列表框级联菜单
  5. Android判断某一应用是否正在运行与判断某一Service是否正在运行
  6. 2019/5/5-Android
  7. android取得当前程序File与Cache路径!
  8. android dialog ——ProgressDialog 进度条对话框详解
  9. ubuntu 10.4 下的 android 编译环境 配置

随机推荐

  1. Xamarin for android:为button设置click
  2. Android推流帧率的设定与实际情况的影响
  3. 自定义控件(一)
  4. Android NDK rb5 文档之使用 Android 工
  5. android应用安装成功之后删除apk文件
  6. Android内存溢出解决方案(OOM)
  7. 修改Android中hosts文件的步骤详解
  8. 评论:Android的升级策略
  9. Android 网络框架之Retrofit2使用详解及
  10. 谷歌之惑:辉煌的Android和尴尬的Nexus On