|
|
|
@ -1,39 +1,88 @@ |
|
|
|
|
# DownloadUtil |
|
|
|
|
# Aria |
|
|
|
|
![图标](https://github.com/AriaLyy/DownloadUtil/blob/v_2.0/app/src/main/res/mipmap-hdpi/ic_launcher.png)</br> |
|
|
|
|
这是一个 android 智能切换多任务断点续传工具,使用该工具,你可以很容易实现`多线程下载功能和复杂的任务自动切换功能`</br> |
|
|
|
|
+ 该工具具有以下特点: |
|
|
|
|
- 通过命令控制下载 |
|
|
|
|
- 可在广播中接收任务的各种下载状态 |
|
|
|
|
Aria,致力于让下载傻瓜化</br> |
|
|
|
|
+ Aria有以下特点: |
|
|
|
|
- 简单 |
|
|
|
|
- 可自定义是否使用广播 |
|
|
|
|
- 支持多线程、多任务下载 |
|
|
|
|
- 支持任务自动切换 |
|
|
|
|
- 支持下载速度直接获取 |
|
|
|
|
|
|
|
|
|
如果你觉得我的代码对你有帮助,您的star和issues将是对我最大支持.`^_^` |
|
|
|
|
[Aria怎样使用?](#使用) |
|
|
|
|
|
|
|
|
|
#下载 |
|
|
|
|
[![Download](https://api.bintray.com/packages/arialyy/maven/MTDownloadUtil/images/download.svg)](https://bintray.com/arialyy/maven/MTDownloadUtil/_latestVersion)<br/> |
|
|
|
|
compile 'com.arialyy.downloadutil:DownloadUtil:2.1.1' |
|
|
|
|
如果你觉得Aria对你有帮助,您的star和issues将是对我最大支持.`^_^` |
|
|
|
|
|
|
|
|
|
## 下载 |
|
|
|
|
[![Download](https://api.bintray.com/packages/arialyy/maven/Aria/images/download.svg)](https://bintray.com/arialyy/maven/Aria/_latestVersion)</br> |
|
|
|
|
compile 'com.arialyy.aria:Aria:2.3.1' |
|
|
|
|
|
|
|
|
|
#示例 |
|
|
|
|
|
|
|
|
|
## 示例 |
|
|
|
|
![多任务下载](https://github.com/AriaLyy/DownloadUtil/blob/v_2.0/img/download_img.gif) |
|
|
|
|
![单任务下载](https://github.com/AriaLyy/DownloadUtil/blob/master/img/11.gif "") |
|
|
|
|
|
|
|
|
|
# 性能展示 |
|
|
|
|
## 性能展示 |
|
|
|
|
![性能展示](https://github.com/AriaLyy/DownloadUtil/blob/v_2.0/img/performance.png) |
|
|
|
|
|
|
|
|
|
# 使用 |
|
|
|
|
* 一、在Application注册下载器 |
|
|
|
|
*** |
|
|
|
|
## 使用 |
|
|
|
|
### 一、Aria 是实体驱动型的工具,所以,第一步,你需要创建一个下载实体 |
|
|
|
|
```java |
|
|
|
|
DownloadEntity mEntity = new DownloadEntity(); |
|
|
|
|
mEntity.setFileName(fileName); //设置文件名 |
|
|
|
|
mEntity.setDownloadUrl(downloadUrl); //设置下载链接 |
|
|
|
|
mEntity.setDownloadPath(downloadPath); //设置存放路径 |
|
|
|
|
``` |
|
|
|
|
### 二、为了能接收到Aria传递的数据,你需要把你的Activity或fragment注册到Aria管理器中,注册的方式很简单,在onResume |
|
|
|
|
```java |
|
|
|
|
public class BaseApplication extends Application { |
|
|
|
|
@Override public void onCreate() { |
|
|
|
|
super.onCreate(); |
|
|
|
|
//注册下载器 |
|
|
|
|
DownloadManager.init(this); |
|
|
|
|
@Override protected void onResume() { |
|
|
|
|
super.onResume(); |
|
|
|
|
Aria.whit(this).addSchedulerListener(new MySchedulerListener()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
``` |
|
|
|
|
|
|
|
|
|
* 二、创建广播接收器,用来接收下载的各种状态 |
|
|
|
|
### 三、还记得上面的DownloadEntity吗?现在是时候使用它进行下载了 |
|
|
|
|
- 启动下载 |
|
|
|
|
```java |
|
|
|
|
Aria.whit(this).load(mEntity).start(); |
|
|
|
|
``` |
|
|
|
|
- 暂停下载 |
|
|
|
|
```java |
|
|
|
|
Aria.whit(this).load(mEntity).stop(); |
|
|
|
|
``` |
|
|
|
|
- 恢复下载 |
|
|
|
|
```java |
|
|
|
|
Aria.whit(this).load(mEntity).resume(); |
|
|
|
|
``` |
|
|
|
|
- 取消下载 |
|
|
|
|
```java |
|
|
|
|
Aria.whit(this).load(mEntity).cancel(); |
|
|
|
|
``` |
|
|
|
|
|
|
|
|
|
### 四、关于Aria,你还需要知道的一些东西 |
|
|
|
|
- 设置下载任务数,Aria默认下载任务为**2** |
|
|
|
|
```java |
|
|
|
|
Aria.get(getContext()).setMaxDownloadNum(num); |
|
|
|
|
``` |
|
|
|
|
- 停止所有下载 |
|
|
|
|
```java |
|
|
|
|
Aria.get(this).stopAllTask(); |
|
|
|
|
``` |
|
|
|
|
- 设置失败重试次数,从事次数不能少于 1 |
|
|
|
|
```java |
|
|
|
|
Aria.get(this).setReTryNum(10); |
|
|
|
|
``` |
|
|
|
|
- 设置失败重试间隔,重试间隔不能小于 5000ms |
|
|
|
|
```java |
|
|
|
|
Aria.get(this).setReTryInterval(5000); |
|
|
|
|
``` |
|
|
|
|
- 设置是否打开广播,如果你需要在Service后台获取下载完成情况,那么你需要打开Aria广播发送按钮,[Aria广播使用](#广播使用) |
|
|
|
|
```java |
|
|
|
|
Aria.get(this).openBroadcast(true); |
|
|
|
|
``` |
|
|
|
|
|
|
|
|
|
*** |
|
|
|
|
### 广播使用 |
|
|
|
|
#### 一、创建广播接收器,用来接收下载的各种状态 |
|
|
|
|
```java |
|
|
|
|
private BroadcastReceiver mReceiver = new BroadcastReceiver() { |
|
|
|
|
long len = 0; |
|
|
|
@ -72,7 +121,7 @@ private BroadcastReceiver mReceiver = new BroadcastReceiver() { |
|
|
|
|
}; |
|
|
|
|
``` |
|
|
|
|
|
|
|
|
|
* 三、在Activity中创建广播过滤器 |
|
|
|
|
#### 二、在Activity中创建广播过滤器 |
|
|
|
|
```java |
|
|
|
|
@Override protected void onResume() { |
|
|
|
|
super.onResume(); |
|
|
|
@ -90,63 +139,16 @@ private BroadcastReceiver mReceiver = new BroadcastReceiver() { |
|
|
|
|
registerReceiver(mReceiver, filter); |
|
|
|
|
} |
|
|
|
|
``` |
|
|
|
|
|
|
|
|
|
* 四、创建下载实体 |
|
|
|
|
```java |
|
|
|
|
DownloadEntity entity = new DownloadEntity(); |
|
|
|
|
entity.setFileName(fileName); //设置文件名 |
|
|
|
|
entity.setDownloadUrl(downloadUrl); //设置下载链接 |
|
|
|
|
entity.setDownloadPath(downloadPath); //设置存放路径 |
|
|
|
|
``` |
|
|
|
|
|
|
|
|
|
* 五、通过命令控制下载(下载状态控制,或下载任务切换将自动完成)</br> |
|
|
|
|
**!!注意:命令需要第四步的下载实体支持** |
|
|
|
|
|
|
|
|
|
- 获取命令工厂实例和下载管理器实例 |
|
|
|
|
```java |
|
|
|
|
CmdFactory factory = CmdFactory.getInstance(); |
|
|
|
|
DownloadManager manager = DownloadManager.getInstance(); |
|
|
|
|
``` |
|
|
|
|
- 开始命令、恢复下载命令都是同一个 |
|
|
|
|
```java |
|
|
|
|
private void start() { |
|
|
|
|
List<IDownloadCmd> commands = new ArrayList<>(); |
|
|
|
|
IDownloadCmd addCMD = factory.createCmd(this, entity, CmdFactory.TASK_CREATE); |
|
|
|
|
IDownloadCmd startCmd = factory.createCmd(this, entity, CmdFactory.TASK_START); |
|
|
|
|
commands.add(addCMD); |
|
|
|
|
commands.add(startCmd); |
|
|
|
|
manager.setCmds(commands).exe(); |
|
|
|
|
} |
|
|
|
|
``` |
|
|
|
|
- 停止命令 |
|
|
|
|
```java |
|
|
|
|
private void stop() { |
|
|
|
|
IDownloadCmd stopCmd = factory.createCmd(this, entity, CmdFactory.TASK_STOP); |
|
|
|
|
manager.setCmd(stopCmd).exe(); |
|
|
|
|
} |
|
|
|
|
``` |
|
|
|
|
- 取消命令(取消下载、删除下载任务) |
|
|
|
|
```java |
|
|
|
|
private void cancel() { |
|
|
|
|
IDownloadCmd cancelCmd = factory.createCmd(this, entity, CmdFactory.TASK_CANCEL); |
|
|
|
|
manager.setCmd(cancelCmd).exe(); |
|
|
|
|
} |
|
|
|
|
``` |
|
|
|
|
|
|
|
|
|
# 修改最大任务数 |
|
|
|
|
``` |
|
|
|
|
mManager.getTaskQueue().setDownloadNum(num); |
|
|
|
|
|
|
|
|
|
``` |
|
|
|
|
|
|
|
|
|
# 开发日志 |
|
|
|
|
*** |
|
|
|
|
## 开发日志 |
|
|
|
|
+ v_2.1.0 修复大量bug |
|
|
|
|
+ v_2.1.1 增加,选择最大下载任务数接口 |
|
|
|
|
+ v_2.3.1 重命名为Aria,下载流程简化 |
|
|
|
|
|
|
|
|
|
License |
|
|
|
|
------- |
|
|
|
|
|
|
|
|
|
Copyright 2016 AriaLyy(DownloadUtil) |
|
|
|
|
Copyright 2016 AriaLyy(https://github.com/AriaLyy/Aria) |
|
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
|
you may not use this file except in compliance with the License. |
|
|
|
|