Getting Started¶
Asynchronous Download¶
Asynchronous download
means executing in an asynchronous thread.
val request = Download.Request.Builder()
.url(url)
.into(file)
.build()
val call = downloader.newCall(request)
call.enqueue()
Add callback listeners
call.enqueue(object : Download.Callback {
// ...
override fun onSuccess(call: Download.Call, response: Download.Response) {
// do your job
}
override fun onFailure(call: Download.Call, response: Download.Response) {
// do your job
}
})
Synchronous Download¶
Synchronous download
means executing in the current thread, blocking the calling thread.
val request = Download.Request.Builder()
.url(url)
.into(file)
.build()
val call = downloader.newCall(request)
val response = call.execute()
Add callback listeners
call.execute(object : Download.Callback {
// ...
override fun onSuccess(call: Download.Call, response: Download.Response) {
// do your job
}
override fun onFailure(call: Download.Call, response: Download.Response) {
// do your job
}
})
Usually, synchronous download is used in coroutines:
withContext(Dispatchers.IO) {
val request = Download.Request.Builder()
.url(url)
.into(file)
.build()
val response = downloader.newCall(request).execute()
}
Canceling Download¶
or
The difference between cancel
and cancelSafely
is that cancelSafely()
will delete the downloaded temporary file.
Canceling all download tasks¶
or
cancelAllSafely
will delete the downloaded temporary files, including the breakpoint file. The next download will start from scratch.
File Verification¶
Set the MD5
value to perform MD5 verification upon download completion
Set the size
of the file to verify the file size upon download completion:
Setting Retries¶
Set the number of retries. The default number of retries is 3:
Setting Priority¶
Supports three priority levels: High, Middle, and Low. The default priority is Middle
Setting Tags¶
Tags are used to label tasks and can be used to differentiate different tasks within the app for reporting purposes:
Task Subscription¶
In addition to task callbacks, task subscription is supported:
val subscriber = object : Download.Subscriber {
override fun onSuccess(call: Download.Call, response: Download.Response) {
// do your job
}
override fun onFailure(call: Download.Call, response: Download.Response) {
// do your job
}
}
downloader.subscribe(subscriber)
Unsubscribe: