FileDownloader下载文件

fileDownloader是一个性能比较高的一个下载库,

优点
多任务下载
多线程下载
断点续传
高并发

话不多说,直接干!
先添加依赖:

compile 'com.liulishuo.filedownloader:library:1.6.4'//最新版本见github

下一步需要在Application中初始化

public class APP extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
       FileDownloader.setup(this);//注意作者已经不建议使用init方法
    }
}

下面是具体用法

FileDownloader.getImpl().create(url).setWifiRequired(true).setPath(path).setListener(new FileDownloadListener() {
            @Override
            protected void pending(BaseDownloadTask task, int soFarBytes, int totalBytes) {

            }

            @Override
            protected void progress(BaseDownloadTask task, int soFarBytes, int totalBytes) {
                int percent=(int) ((double) soFarBytes / (double) totalBytes * 100);
                textView.setText("("+percent+"%"+")");
            }

            @Override
            protected void blockComplete(BaseDownloadTask task) {

            }

            @Override
            protected void completed(BaseDownloadTask task) {
                Toast.makeText(MainActivity.this,"下载完成!",Toast.LENGTH_SHORT).show();
                textView.setText("("+"100%"+")");
            }

            @Override
            protected void paused(BaseDownloadTask task, int soFarBytes, int totalBytes) {

            }

            @Override
            protected void error(BaseDownloadTask task, Throwable e) {

            }

            @Override
            protected void warn(BaseDownloadTask task) {
                continueDownLoad(task);//如果存在了相同的任务,那么就继续下载
            }
        }).start();

private void continueDownLoad(BaseDownloadTask task) {
        while (task.getSmallFileSoFarBytes()!=task.getSmallFileTotalBytes()){
            int percent=(int) ((double) task.getSmallFileSoFarBytes() / (double) task.getSmallFileTotalBytes() * 100);
            textView.setText("("+percent+"%"+")");
        }
    }

具体的也可以参考
https://github.com/lingochamp/FileDownloader/blob/master/README-zh.md

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值