Android XUtils单线程断点续传

第一步:导入Xutils Jar包

第二部:添加权限

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
Activity:

  1. public class MainActivity extends AppCompatActivity implements View.OnClickListener {  
  2.   
  3.     private ProgressBar pb;  
  4.     private TextView tv_error;  
  5.     private TextView tv_progress;  
  6.     private Button btn_down,btn_stop;  
  7.   
  8.     Boolean isDowloding = false;  
  9.     HttpHandler handler;  
  10.   
  11.     @Override  
  12.     protected void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.activity_main);  
  15.   
  16.         // 初始化  
  17.         pb = (ProgressBar) findViewById(R.id.pb);  
  18.         tv_progress = (TextView) findViewById(R.id.tv_progress);  
  19.         tv_error = (TextView) findViewById(R.id.tv_failure);  
  20.         btn_down = (Button) findViewById(R.id.btn_down);  
  21.         btn_stop = (Button) findViewById(R.id.btn_stop);  
  22.   
  23.         // 开始点击事件  
  24.         btn_down.setOnClickListener(this);  
  25. //        //暂停点击事件  
  26.         btn_stop.setOnClickListener(this);  
  27.     }  
  28.   
  29.   
  30.   
  31.   
  32.     @Override  
  33.     public void onClick(View view) {  
  34.         //String fileName = "weixin_821.apk";  
  35.   
  36.         //String fileName = "bbcc.jpg";  
  37.   
  38. //String path ="http://c.hiphotos.baidu.com/image/pic/item/b90e7bec54e736d1e51217c292504fc2d46269f3.jpg";  
  39.   
  40.   
  41.         switch (view.getId()){  
  42.             case R.id.btn_down:  
  43.   
  44.                 btn_stop.setEnabled(true);  
  45.                 btn_down.setEnabled(false);  
  46.                 Toast.makeText(this"开始", Toast.LENGTH_SHORT).show();  
  47.                 String fileName = "bbbbb.jpg";  
  48.   
  49.                 String path ="http://c.hiphotos.baidu.com/image/pic/item/b90e7bec54e736d1e51217c292504fc2d46269f3.jpg";  
  50.                 //String path = "http://gdown.baidu.com/data/wisegame/df65a597122796a4/" + fileName;  
  51.                 HttpUtils http = new HttpUtils();  
  52.                 handler = http.download(path, Environment.getExternalStorageDirectory() + "/"  
  53.                         + fileName, truetruenew RequestCallBack<File>() {  
  54.   
  55.                     @Override  
  56.                     public void onSuccess(ResponseInfo<File> arg0) {  
  57.                         isDowloding = false;  
  58.                         // 下载成功  
  59.                         Toast.makeText(MainActivity.this, arg0.result.getPath(), Toast.LENGTH_SHORT).show();  
  60.                     }  
  61.   
  62.                     @Override  
  63.                     public void onFailure(HttpException arg0, String arg1) {  
  64.                         // 下载失败  
  65.                         tv_error.setText(arg1);  
  66.                     }  
  67.   
  68.                     @Override  
  69.                     public void onLoading(long total, long current, boolean isUploading) {  
  70.                         super.onLoading(total, current, isUploading);  
  71.                         if (current < total) {  
  72.                             isDowloding = true;  
  73.                         } else {  
  74.                             isDowloding = false;  
  75.                         }  
  76.                         // 下载任务  
  77.                         pb.setMax((int) total);  
  78.                         pb.setProgress((int) current);  
  79.                         tv_progress.setText(current * 100 / total + "%");  
  80.                     }  
  81.   
  82.                 });  
  83.   
  84.                 break;  
  85.             case R.id.btn_stop:  
  86.   
  87.                 btn_stop.setEnabled(false);  
  88.                 btn_down.setEnabled(true);  
  89.                 if (isDowloding) {  
  90.                     if (handler != null) {  
  91.                         handler.cancel();  
  92.                     }  
  93.                 }  
  94.   
  95.                 break;  
  96.         }  
  97.   
  98.   
  99.     }  
  100. }  
第四步:xml

  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:orientation="vertical"  
  8.     tools:context=".MainActivity">  
  9.   
  10.     <LinearLayout  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         android:orientation="horizontal">  
  14.   
  15.         <Button  
  16.             android:id="@+id/btn_down"  
  17.             android:layout_width="wrap_content"  
  18.             android:layout_height="wrap_content"  
  19.             android:text="开始下载" />  
  20.   
  21.         <Button  
  22.             android:id="@+id/btn_stop"  
  23.             android:layout_width="wrap_content"  
  24.             android:layout_height="wrap_content"  
  25.             android:text="暂停下载" />  
  26.     </LinearLayout>  
  27.   
  28.     <TextView  
  29.         android:id="@+id/tv_failure"  
  30.         android:layout_width="wrap_content"  
  31.         android:layout_height="wrap_content" />  
  32.   
  33.     <ProgressBar  
  34.         android:id="@+id/pb"  
  35.         style="@android:style/Widget.ProgressBar.Horizontal"  
  36.         android:layout_width="match_parent"  
  37.         android:layout_height="wrap_content" />  
  38.   
  39.     <TextView  
  40.         android:id="@+id/tv_progress"  
  41.         android:layout_width="wrap_content"  
  42.         android:layout_height="wrap_content"  
  43.         android:text="下载进度" />  
  44. </LinearLayout>  

猜你喜欢

转载自blog.csdn.net/taa1007/article/details/78519415