ThinDownloadManager
===================
Thin DownloadManager is an android library primary to download files and to avoid using *DOWNLOAD_WITHOUT_NOTIFICATION* permission when using Android provided DownloadManager in your application.
## Why ?
There are few reasons why you might want to use this library.
* There are situations where you wanted to download a file into application's sandboxed cache or files directory where no one else can access to. **DownloadManager** provided by android does not have facility to download directly to application's cache or files directory **(/data/data/<package>/cache/ or /data/data/<pacakge>/files/)**. It can only accept destination in external SDcard as download destination. And if you are not using application's external file directory as destination i.e *(setDestinationInExternalFilesDir())* you have to have *android.permission.WRITE_EXTERNAL_STORAGE*
Most of the times we download using Android's DownloadManager to external files directory and upon successful completion move the downloaded file to the sandboxed application's cache/file directory to avoid writing a own download manager which is a bit tedious. This library is handy in such situations.
* **No additional permissions required.** Any download initiated by your application using android DownloadManager would throw a progress notification on status bar letting user know that you are downloading a file. So you end up using *setVisibleInDownloadsUi(false)* & having this permission *android.permission.DOWNLOAD_WITHOUT_NOTIFICATION*. When users install your app, they would be shown this permission and it makes them scary not to install your app because you are downloading some files without user's notification. Why give a chance of user not installing your app for this permission. You definetly need this library in this case.
* **Volley** - Google recommended Networking library for android doesn't have options to download a file.
## Usuage
#### **DownloadStatusListener (Deprecated)**
* Provides call back option to know when the download is completed, failed and reason for failure, and to know the progress of the download.
``` java
//Callback when download is successfully completed
void onDownloadComplete (int id);
//Callback if download is failed. Corresponding error code and
//error messages are provided
void onDownloadFailed (int id, int errorCode, String errorMessage);
//Callback provides download progress
void onProgress (int id, long totalBytes, long downlaodedBytes, int progress);
```
#### **DownloadStatusListenerV1**
* Provides call back option to know when the download is completed, failed and reason for failure, and to know the progress of the download. DownloadRequest is given back in the callback so that you can easily set some Object as context to download request and get the context object back from the request object.
``` java
//Callback when download is successfully completed
void onDownloadComplete(DownloadRequest downloadRequest);
//Callback if download is failed. Corresponding error code and
//error messages are provided
void onDownloadFailed(DownloadRequest downloadRequest, int errorCode, String errorMessage);
//Callback provides download progress
void onProgress(DownloadRequest downloadRequest, long totalBytes, long downloadedBytes, int progress);
```
#### **DownloadRequest**
* Takes all the necessary information required for download.
* Download URI, Destination URI.
* Set Priority for request as HIGH or MEDIUM or LOW.
* Takes Callback listener DownloadStatusListener
* Use custom Http Headers for a download request
* Resumable a download if network connection drops or download is paused.
* You can set a Retry Policy
``` java
Uri downloadUri = Uri.parse("http://tcrn.ch/Yu1Ooo1");
Uri destinationUri = Uri.parse(this.getExternalCacheDir().toString()+"/test.mp4");
DownloadRequest downloadRequest = new DownloadRequest(downloadUri)
.addCustomHeader("Auth-Token", "YourTokenApiKey")
.setRetryPolicy(new DefaultRetryPolicy())
.setDestinationURI(destinationUri).setPriority(DownloadRequest.Priority.HIGH)
.setDownloadContext(downloadContextObject)//Optional
.setDownloadListener(new DownloadStatusListener() {
@Override
public void onDownloadComplete(int id) {
}
@Override
public void onDownloadFailed(int id, int errorCode, String errorMessage) {
}
@Override
public void onProgress(int id, long totalBytes, long downlaodedBytes, int progress)) {
}
});
```
#### **ThinDownloadManager**
* The number of threads used to perform parallel download is determined by the available processors on the device. Uses `Runtime.getRuntime().availableProcessors()` api.
``` java
private ThinDownloadManager downloadManager;
.....
downloadManager = new ThinDownloadManager();
....
```
* To start a download use *add( DownloadRequest request)*
```java
int downloadId = downloadManager.add(downloadRequest);
```
* To cancel a particular download use *cancel(int downloadId)* by passing download id.
- Returns 1 if successfull cancelled.
- Returns -1 if supplied download id is not found.
```java
int status = downloadManager.cancel(downloadId);
```
* To cancel all running requests use *cancelAll()*
```java
downloadManager.cancelAll();
```
* To query for a particular download use *query(int downloadId)*
The possible status could be
- STATUS_PENDING
- STATUS_STARTED
- STATUS_RUNNING
```java
int status = downloadManager.query(downloadId);
```
* To pause a download in progress. The download request has to be marked as `setDownloadResumable` to true
``` java
downloadManager.pause(downloadId)
```
* To release all the resources used by download manager use *release()*.
```java
downloadManager.release();
```
## No Permissions Required
* Unless if you specify download destination to be in external public SDCard location.You might need *android.permission.WRITE_EXTERNAL_STORAGE* permission.
## Setup
Include below line your build.gradle:
```java
dependencies {
compile 'com.mani:ThinDownloadManager:1.4.0'
}
```
Make sure you included jcenter() in your repositories section.
## Download
* The source code of sample app code is available for you to play around and the app itself is available for download from play store :
<a href="https://play.google.com/store/apps/details?id=com.mani.thindownloadmanager.app&utm_source=global_co&utm_medium=prtnr&utm_content=Mar2515&utm_campaign=PartBadge&pcampaignid=MKT-AC-global-none-all-co-pr-py-PartBadges-Oct1515-1"><img alt="Get it on Google Play" width="100" height="50" src="https://play.google.com/intl/en_us/badges/images/apps/en-play-badge.png" ></a>
* Sample app demonstrates with 4 thread pool size and download three different formats of files jpg, mp3, mp4.
* The files are downloaded to applications sandboxed files directory. */data/data/<package>/files.
[](https://android-arsenal.com/details/1/2393)
## Credits
https://android.googlesource.com/platform/packages/providers/DownloadProvider/
NOTE: Android's DownloadManager has plenty of features which is not available in ThinDownloadManager. For ex. pause and continue download when network connectivity changes.So analyse your requirement thoroughly and decide which one to use.
https://www.virag.si/2015/01/publishing-gradle-android-library-to-jcenter/
Steps for uploading to bintray.
## License
```
Copyright 2013 Mani Selvaraj
Licensed under

清净平常心
- 粉丝: 42
- 资源: 4671
最新资源
- 线控转向系统:基于Carsim与Simulink联合仿真的控制策略与模型研究,线控转向系统:基于Carsim与Simulink联合仿真的控制策略与模型研究,线控转向系统联合仿真模型及控制策略研究 描述
- 基于python的订单管理系统
- 基于新算法SSA优化的变分模态分解风电功率分配策略:混合储能高效利用,创新性显著,基于新算法SSA优化的变分模态分解风电功率分配策略:混合储能高效利用与参数优化策略,基于新算法SSA优化变分模态分解的
- 【bussiness-DC】
- 使用BE、FE及C N方法求解一维扩散方程的Matlab实现,使用BE(向后欧拉)与FE(向前欧拉)算法及C N方法在Matlab中求解一维扩散方程的数值分析研究,使用BE(向后欧拉),FE(向前欧拉
- 博图16 PLC控制的4x5立体车库系统:包含运行效果视频、接线图及IO表全解析,4x5立体车库控制系统详解:博图16 PLC运行效果视频、接线图及IO表一览,4x5立体车库控制系统 博图16 带PL
- Python 智能机房签到系统:高效管理课堂考勤
- 进程上下文详细分析PDF原文
- 基于拉丁超立方采样与自适应核密度估计的电力系统概率潮流精准计算,基于拉丁超立方采样的电力系统概率潮流计算:自适应核密度估计与带宽优化的方法研究,采用拉丁超立方采样的电力系统概率潮流计算 (自适应核密度
- numpy-2.2.0-cp310-cp310-win32.whl
- numpy-2.2.0-cp310-cp310-win_amd64.whl
- 仅作测试无意义1111111111111111
- 基于PWM的 三色灯RGB模块调色 HAL库
- 商业数据分析BI+人工智能AI.zip
- 伺服驱动系统中的FPGA实现:电流环、速度环与位置环的协调控制,包含坐标变换及电机反馈接口,SVPWM技术,以及在FPGA中实现的编码器协议,伺服驱动FPGA电流环:实现坐标变换、电机反馈与SVPWM
- 基于PWM的 三色灯RGB模块调色 寄存器 代码
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



评论1