Git Product home page Git Product logo

download's Introduction

在Android原生APP开发中,文件下载是一个常用的模块,结合Retrofit2和Rxjava2封装了一个下载管理器。

1. 创建下载任务

DownloadTask task = new DownloadTask.Builder()
                .setUrl(url)
                .setLocalPath(path)
	        .setPriority(1)
                .setDonwloadListener(listener)
                .build();

在调用build()方法时会对参数进行检查,所以build()需放到最后调用且下载地址和本地文件路径不能为null

2. 提交下载任务

DownloadManager是下载队列的管理类,提供了一个下载任务的方法:

DownloadFuture download(@NonNull DownloadTask task)

为了方便使用,还提供了另一个方法:

DownloadFuture download(String url, String path, DownloadListener listener)

3. 任务的暂停和取消

任务的暂停和取消通过DownloadFuture来管理的:

// 暂停
void pause()
// 恢复
void resume()
// 取消
boolean cancel(boolean mayInterruptIfRunning)

4. 效果如下

download's People

Contributors

zhoukang99 avatar

Stargazers

 avatar

Forkers

onlychunxiao

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.