多线程下载以及断点续传...
来源:互联网 发布:淘宝哪家女装质量好 编辑:程序博客网 时间:2024/05/21 10:07
Mainactivity
----------------------------------------------------------------------------
package com.example.duoxiancheng;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.MediaController;import android.widget.ProgressBar;import android.widget.TextView;import android.widget.VideoView;import java.io.File;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 MainActivityduoxiancheng extends AppCompatActivity implements View.OnClickListener { /** * 0% */ private TextView mDownload; /** * 暂停 */ private Button mStop; private ProgressBar mPb; /** * 添加下载 */ private Button mStart; private Subscription subscribe; //handler线程操作 private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); DownloadStatus downloadStatus = (DownloadStatus) msg.obj; //获得总文件的大小 long totalSize = downloadStatus.getTotalSize(); long downloadSize = downloadStatus.getDownloadSize(); String percent = downloadStatus.getPercent();//完成的百分比 long t = totalSize / 100; int tt = (int) (downloadSize / t); if (downloadSize <= totalSize) { mDownload.setText(percent); mPb.setProgress(tt); } } }; private VideoView mVv; private MediaController mediaco; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_activityduoxiancheng); initView(); } //初始化组件 private void initView() { mDownload = (TextView) findViewById(R.id.download); mStop = (Button) findViewById(R.id.stop); mStop.setOnClickListener(this); mPb = (ProgressBar) findViewById(R.id.pb); mStart = (Button) findViewById(R.id.start); mStart.setOnClickListener(this); // mVv = (VideoView) findViewById(R.id.vv); mediaco = new MediaController(this); File file = new File("/sdcard/Download/zz.mp4"); if (file.exists()) { //VideoView与MediaController进行关联 mVv.setVideoPath(file.getAbsolutePath()); mVv.setMediaController(mediaco); mediaco.setMediaPlayer(mVv); //让VideiView获取焦点 mVv.requestFocus(); } } //点击事件 @Override public void onClick(View v) { switch (v.getId()) { case R.id.stop: if (subscribe != null && !subscribe.isUnsubscribed()) { subscribe.unsubscribe(); } break; case R.id.start: String url = "http://169.254.222.173:8080/OkHttp/files/gao.mp4"; subscribe = RxDownload.getInstance() //设置总线程数为4个 .maxThread(6) .maxRetryCount(10) .download(url, "zz.mp4", null) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Subscriber<DownloadStatus>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(DownloadStatus downloadStatus) { Message message = new Message(); message.obj = downloadStatus; handler.sendMessageDelayed(message, 100); } }); break; } }}
layout
-------------------------------------------------------------------------------------------------------
<?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.duoxiancheng.MainActivityduoxiancheng"> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_weight="1" android:layout_gravity="center_vertical" android:orientation="vertical" android:layout_marginLeft="15dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:text="aa" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/download" android:layout_marginTop="5dp" android:text="0%" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <Button android:id="@+id/stop" android:text="暂停" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <ProgressBar android:id="@+id/pb" style="@style/Widget.AppCompat.ProgressBar.Horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" /> <LinearLayout android:orientation="horizontal" android:gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/start" android:text="添加下载" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:text="用户:zyx" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout></LinearLayout>依赖-------------------------------compile 'zlc.season:rxdownload:1.1.0'compile'org.greenrobot:greendao:3.0.1'compile'org.greenrobot:greendao-generator:3.0.0'compile 'com.github.leifzhang:IjkLib:0.4.3'
权限---------------------------------------------------<uses-permission android:name="android.permission.INTERNET"></uses-permission><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
阅读全文
0 0
- 多线程下载以及断点续传
- 多线程下载以及断点续传...
- android多线程下载以及断点续传
- android多线程下载以及断点续传
- android多线程下载以及断点续传
- android多线程下载以及断点续传
- Android【封装】多线程下载以及断点续传
- 多线程断点续传后台下载
- 多线程断点续传后台下载
- android 多线程断点续传下载
- 多线程断点续传后台下载
- 多线程并行下载,断点续传
- Android多线程.断点续传下载
- android 多线程断点续传下载
- Android 多线程下载断点续传
- Java多线程断点续传下载
- 多线程断点续传后台下载
- Android多线程断点续传下载
- 三个事和三个问题
- 第九章0.4
- Camel可视化操作(结合Gooflow)
- 动态规划——青蛙过桥
- Mac IDEA快捷键精华版
- 多线程下载以及断点续传...
- String的复习
- 总结线性表
- #pragma 详解
- 注入
- 类型的生命周期
- Android 属性动画:属性动画 总结&攻略
- Python机器学习库sklearn构造分段与多项式特征
- Matlab实现meanshift算法,目标跟踪代码实现