OkDownload Documentation
repository·master·Indexed 26 days ago
https://github.com/lingochamp/okdownloadA reliable, flexible, and high-performance download engine for Android and successor to FileDownloader2. It supports simple and advanced use cases, including managing download queues, parallel task configuration, and custom component injection. The library provides Kotlin extensions for concise DownloadListener creation, coroutine support via .await(), and progress tracking through Kotlin Channels.
What's inside OkDownload
- OkDownload is a reliable, flexible, high-performance, and powerful download engine for Android. It is the successor to FileDownloader2, inheriting its advantages while providing further optimizations and additional features.
Use OkDownload for advanced configurations
masterAdvanced usage scenarios include:
- Setting the maximum number of parallel running tasks.
- Setting the delay (in milliseconds) for submitting tasks to the database.
- Injecting custom components.
Detailed instructions are available in the Advanced Use Guideline in the project wiki.
Use OkDownload for simple download tasks
masterFor basic usage scenarios, including starting a download, canceling a task, downloading via a queue, or retrieving task status and information, consult the Simple Use Guideline in the project wiki.Import OkDownload into your project
masterOkDownload is available on several repositories. You can find it in:
- JCenter
- MavenCentral
- Sonatype's snapshots repository
For specific dependency configuration instructions, refer to the official wiki.
Start a DownloadTask using Kotlin extension functions
masterThe library provides Kotlin extension methods on
DownloadTaskto start tasks and handle listeners in a single call. This eliminates the need to manually create listener objects.lateinit var task: DownloadTask // start task with a listener via extension function task.enqueue { task, cause, realCause -> }Use OkDownload
masterOkDownload provides different levels of usage depending on your requirements:
Simple Use Cases
Use the simple guidelines for:
- Starting and canceling downloads
- Managing download queues
- Retrieving task state or task information
Advanced Use Cases
Use the advanced guidelines for:
- Setting the maximum number of parallel running tasks
- Setting database delay milliseconds
- Injecting custom components
Import OkDownload
masterOkDownload is available on several Maven repositories. You can import it via JCenter, MavenCentral, or Sonatype's snapshots repository. For specific dependency configuration details, refer to the official wiki.Debug OkDownload
masterIf you need to debug the download engine, follow the debugging guide provided in the project wiki.Show download progress using listeners
masterTo show progress using the listener pattern, use the
enqueue3extension method and provide anonProgresslambda.lateinit var task: DownloadTask task.enqueue3( onProgress = { task, currentOffset, totalLength -> // show progress } ) { task, cause, realCause -> }Show download progress using Kotlin Channels
masterYou can observe download progress via a Kotlin Channel. After starting the task (e.g., via
enqueue3), calltask.spChannel()to get the channel and iterate over it within a coroutine.lateinit var task: DownloadTask // 1. Start the task task.enqueue3 { task, cause, realCause -> } // 2. Access the channel (must be after starting task) val pb = task.spChannel() lifecycleScope.launch { for (progress in pb) { // show progress } }Start a DownloadTask using Kotlin Coroutines
masterYou can use the
.await()extension method on aDownloadTaskwithin a coroutine scope to suspend until the download is complete.lateinit var task: DownloadTask // start task and await result in a coroutine lifecycleScope.launch { val downloadResult = task.await() }Create a DownloadListener3
masterUse
createListener3for a highly concise listener where the terminal block is of type() -> Unit. This block is invoked wheneverwarn,error,completed, orcanceledoccurs. To handle progress updates, provide theonProgressparameter.// Only a terminal block is necessary val listener3 = createListener3 {} // Including progress callback val listener3 = createListener3( onProgress = { task, currentOffset, totalLength -> } ) {}