多线程下载以及断点续传...

来源:互联网 发布:淘宝哪家女装质量好 编辑:程序博客网 时间: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>

原创粉丝点击