Ketch 是一个简单、强大、可定制的 Android 文件下载器库,完全用 Kotlin 构建。它利用 WorkManager 的强大功能简化了 Android 应用程序中下载文件的过程。无论应用程序状态如何,Ketch 都会保证下载。
要将 Ketch 库集成到您的 Android 项目中,请按照以下简单步骤操作:
dependencyResolutionManagement {
repositoriesMode . set( RepositoriesMode . FAIL_ON_PROJECT_REPOS )
repositories {
google()
mavenCentral()
maven { url ' https://jitpack.io ' } // this one
}
}
dependencies {
implementation ' com.github.khushpanchal:Ketch:2.0.2 ' // Use latest available version
}
使用 Ketch 最简单的方法:
在应用程序 onCreate 中创建 Ketch 实例。 (Ketch 是一个单例类,实例将在第一次使用时自动创建)
private lateinit var ketch : Ketch
override fun onCreate () {
super .onCreate()
ketch = Ketch .builder().build( this )
}
调用download()函数,传递url、文件名、路径并观察下载状态
val id = ketch.download(url, fileName, path)
lifecycleScope.launch {
repeatOnLifecycle( Lifecycle . State . STARTED ) {
ketch.observeDownloadById(id)
.flowOn( Dispatchers . IO )
.collect { downloadModel ->
// use downloadModel
}
}
}
取消下载
ketch.cancel(downloadModel.id) // other options: cancel(tag), cancelAll()
暂停下载
ketch.pause(downloadModel.id) // other options: pause(tag), pauseAll()
恢复下载
ketch.resume(downloadModel.id) // other options: resume(tag), resumeAll()
重试下载
ketch.retry(downloadModel.id) // other options: retry(tag), retryAll()
删除下载
ketch.clearDb(downloadModel.id) // other options: clearDb(tag), clearAllDb(), clearDb(timeInMillis)
ketch.clearDb(downloadModel.id, false ) // Pass "false" to skip the actual file deletion (only clear entry from DB)
观察:提供下载项的状态流(每个项携带下载信息,如 url、文件名、路径、标签、id、timeQueued、状态、进度、长度、速度、lastModified、metaData、failureReason、eTag)
// To observe from Fragment
viewLifecycleOwner.lifecycleScope.launch {
repeatOnLifecycle( Lifecycle . State . STARTED ) {
ketch.observeDownloads()
.flowOn( Dispatchers . IO )
.collect {
// set items to adapter
}
}
}
要启用通知:
在清单文件中添加通知权限。
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
请求用户许可(从 Android 13(API 级别 33)开始需要)。查看示例应用程序以供参考。
初始化时传递通知配置
ketch = Ketch .builder().setNotificationConfig(
config = NotificationConfig (
enabled = true ,
smallIcon = R .drawable.ic_launcher_foreground // It is required to pass the smallIcon for notification.
)
).build( this )
提供网络请求的标头。
ketch.download(url, fileName, path,
headers = headers, // Default: Empty hashmap
)
标签:通过提供附加标签对各种下载进行分组。 (此标签还可用于取消、暂停、恢复、删除下载)
ketch.download(url, fileName, path,
tag = tag, // Default: null
)
下载配置:提供自定义连接和读取超时
ketch = Ketch .builder().setDownloadConfig(
config = DownloadConfig (
connectTimeOutInMs = 20000L , // Default: 10000L
readTimeOutInMs = 15000L // Default: 10000L
)
).build( this )
自定义OKHttp:提供自定义okhttp客户端
ketch = Ketch .builder().setOkHttpClient(
okHttpClient = OkHttpClient
. Builder ()
.connectTimeout( 10000L )
.readTimeout( 10000L )
.build()
).build( this )
通知配置:提供自定义通知配置
ketch = Ketch .builder().setNotificationConfig(
config = NotificationConfig (
enabled = true , // Default: false
channelName = channelName, // Default: "File Download"
channelDescription = channelDescription, // Default: "Notify file download status"
importance = importance, // Default: NotificationManager.IMPORTANCE_HIGH
smallIcon = smallIcon, // It is required
showSpeed = true , // Default: true
showSize = true , // Default: true
showTime = true // Default: true
)
).build( this )
查看博客以了解 Ketch(高级设计)的工作原理:https://medium.com/@khush.panchal123/ketch-android-file-downloader-library-7369f7b93bd1
请随意提供反馈、报告问题或为 Ketch 做出贡献。前往 GitHub 存储库,创建问题或查找待处理的问题。欢迎所有请求?
Copyright (C) 2024 Khush Panchal
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.