多线程下载断点续传

1.首先配置GreenDao网上好多博客,参照就好了


2.配置网络权限及其他依赖

3.布局

<?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.ccm.morethreaddownload.activity.MainActivity">

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="下载"
        />
    <ProgressBar
        android:id="@+id/pro"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_margin="20dp"
        />

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>


4.GreenDao的bean实体类

@Entity
public class Bean {
    @Id(autoincrement = true)
    long id;
    String downpath;
    int threadid;
    int downlength;
    public int getDownlength() {
        return this.downlength;
    }
    public void setDownlength(int downlength) {
        this.downlength = downlength;
    }
    public int getThreadid() {
        return this.threadid;
    }
    public void setThreadid(int threadid) {
        this.threadid = threadid;
    }
    public String getDownpath() {
        return this.downpath;
    }
    public void setDownpath(String downpath) {
        this.downpath = downpath;
    }
    public long getId() {
        return this.id;
    }
    public void setId(long id) {
        this.id = id;
    }
    @Generated(hash = 635468303)
    public Bean(long id, String downpath, int threadid, int downlength) {
        this.id = id;
        this.downpath = downpath;
        this.threadid = threadid;
        this.downlength = downlength;
    }
    @Generated(hash = 80546095)
    public Bean() {
    }
}




5.MainActivity

public class MainActivity extends AppCompatActivity {
    private FileDownloader loader;
    //声明控件
    @InjectView(R.id.btn)
    Button btn;
    @InjectView(R.id.pro)
    ProgressBar progressBar;
    @InjectView(R.id.tv)
    TextView tv;

    /**
     * 当Handler被创建会关联到创建它的当前线程的消息队列,该类用于往消息队列发送消息
     * 消息队列中的消息由当前线程内部进行处理
     */
    private Handler handler = new Handler(){

        @Override
        public void handleMessage(Message msg) {

            switch (msg.what) {
                case 1:
                    long size = msg.getData().getInt("size");
                    long fileSize = loader.getFileSize();
                    float v = size  * 100 / fileSize;
                    progressBar.setProgress((int) size);
                    tv.setText(v+"%");
                    if(progressBar.getProgress()==progressBar.getMax()){
                        Toast.makeText(MainActivity.this, R.string.success, Toast.LENGTH_SHORT).show();
                    }
                    break;
                case -1:
                    Toast.makeText(MainActivity.this, R.string.error, Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化
        ButterKnife.inject(this);
        //切换文字
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(btn.getText().toString().equals("下载")){
                    btn.setText("
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值