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.