先导依赖
compile 'zlc.season:rxdownload:1.1.1'
清单文件
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Activity代码
package com.example.cll.caolilu1508b20171113; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.ProgressBar; import android.widget.TextView; import rx.Subscriber; import rx.Subscription; import rx.android.schedulers.AndroidSchedulers; import rx.schedulers.Schedulers; import zlc.season.rxdownload.DownloadStatus; import zlc.season.rxdownload.RxDownload; public class Main2Activity extends AppCompatActivity implements View.OnClickListener { /** * 下载 */ private Button mXaizai; /** * 暂停 */ private Button mZanting; private ProgressBar mJindu; /** * 下载:0% */ private TextView mTvInfo; private String uri = "http://mirror.aarnet.edu.au/pub/TED-talks/911Mothers_2010W-480p.mp4"; private String name="cllshipin"; private String dizhi; private Subscription subscription; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); initView(); } private void initView() { mXaizai = (Button) findViewById(R.id.xaizai); mXaizai.setOnClickListener(this); mZanting = (Button) findViewById(R.id.zanting); mZanting.setOnClickListener(this); mJindu = (ProgressBar) findViewById(R.id.jindu); mTvInfo = (TextView) findViewById(R.id.tv_info); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.xaizai: mXaizai.setEnabled(false); mZanting.setEnabled(true); subscription = RxDownload.getInstance() .maxThread(20) .maxRetryCount(5) .download(uri,name,dizhi) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Subscriber() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(DownloadStatus downloadStatus) { String percent = downloadStatus.getPercent(); mTvInfo.setText("下载:"+percent); long totalSize = downloadStatus.getTotalSize(); mJindu.setMax((int) totalSize); long downloadSize = downloadStatus.getDownloadSize(); mJindu.setProgress((int) downloadSize); } }); break; case R.id.zanting: mXaizai.setEnabled(true); mZanting.setEnabled(false); //...//取消订阅, 即可暂停下载, 若服务端不支持断点续传,下一次下载会重新下载,反之会继续下载 if (subscription != null && !subscription.isUnsubscribed()) { subscription.unsubscribe(); } break; } } } 布局文件
xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.cll.caolilu1508b20171113.Main2Activity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="下载地址"/> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/xaizai" android:text="下载" android:onClick="download"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/zanting" android:onClick="pause" android:text="暂停"/> LinearLayout> <ProgressBar android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/jindu" style="@android:style/Widget.ProgressBar.Horizontal" /> <TextView android:id="@+id/tv_info" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:text="下载:0%" /> LinearLayout>