import android.net.Uri;
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.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button downloadBtn1, downloadBtn2, downloadBtn3;
private Button cancelBtn1, cancelBtn2, cancelBtn3;
private ProgressBar progress1, progress2, progress3;
private String url1 = "http://dl175.80s.im:920/1803/西游记女儿国/西游记女儿国.mp4";
private String url2 = "http://dl174.80s.im:920/1803/湮灭/湮灭.mp4";
private String url3 = "http://dl173.80s.im:920/1803/南极之恋/南极之恋.mp4";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
downloadBtn1 = findViewById(R.id.main_btn_down1);
downloadBtn2 = findViewById(R.id.main_btn_down2);
downloadBtn3 = findViewById(R.id.main_btn_down3);
cancelBtn1 = findViewById(R.id.main_btn_cancel1);
cancelBtn2 = findViewById(R.id.main_btn_cancel2);
cancelBtn3 = findViewById(R.id.main_btn_cancel3);
progress1 = findViewById(R.id.main_progress1);
progress2 = findViewById(R.id.main_progress2);
progress3 = findViewById(R.id.main_progress3);
downloadBtn1.setOnClickListener(this);
downloadBtn2.setOnClickListener(this);
downloadBtn3.setOnClickListener(this);
cancelBtn1.setOnClickListener(this);
cancelBtn2.setOnClickListener(this);
cancelBtn3.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.main_btn_down1:
DownloadManager.getInstance().download(url1, new DownLoadObserver() {
@Override
public void onNext(DownloadInfo value) {
super.onNext(value);
progress1.setMax((int) value.getTotal());
progress1.setProgress((int) value.getProgress());
}
@Override
public void onComplete() {
if(downloadInfo != null){
Toast.makeText(MainActivity.this,
downloadInfo.getFileName() + "-DownloadComplete",
Toast.LENGTH_SHORT).show();
}
}
});
break;
case R.id.main_btn_down2:
DownloadManager.getInstance().download(url2, new DownLoadObserver() {
@Override
public void onNext(DownloadInfo value) {
super.onNext(value);
progress2.setMax((int) value.getTotal());
progress2.setProgress((int) value.getProgress());
Android--okhttp断点下载+进度
最新推荐文章于 2021-05-26 19:36:38 发布
本文介绍如何使用OkHttp在Android中实现断点续传下载功能,并展示下载进度。通过创建一个实用的下载工具类,结合RxJava进行任务订阅和进度更新。
摘要由CSDN通过智能技术生成