package com.example.administrator.zengyuxin1508b20171113;

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.ProgressBar;
import android.widget.TextView;

import com.example.administrator.zengyuxin1508b20171113.gen.DaoMaster;
import com.example.administrator.zengyuxin1508b20171113.gen.DaoSession;
import com.example.administrator.zengyuxin1508b20171113.gen.UserDao;

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 MainActivity 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();

            if(downloadSize<=totalSize){
                mDownload.setText(percent);
                mPb.setProgress((int) downloadSize / (1024 * 1024));
                User user = new User(null,percent);
                userDao.insert(user);

                //完全打印 *打印卡死了
/*                List<User> users = userDao.loadAll();
                for (int i=0;i<users.size();i++){
                    Log.i("xxx",users.toString());
                }*/
            }
        }
    };
    private UserDao userDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(MyApp.getContext(), "my_db", null);
        DaoMaster daoMaster = new DaoMaster(devOpenHelper.getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        userDao = daoSession.getUserDao();
    }

    //初始化组件
    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);
    }

    //点击事件
    @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://mirror.aarnet.edu.au/pub/TED-talks/911Mothers_2010W-480p.mp4";
                subscribe = RxDownload.getInstance()
                        //设置总线程数为4个
                        .maxThread(4)
                        .maxRetryCount(10)
                        .download(url, "shipin.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;
        }
    }
}
//布局
<?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.administrator.zengyuxin1508b20171113.MainActivity">

    <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:layout_marginTop="400dp"
        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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值