其他
使用 Kotlin API 实践 WorkManager
WorkManager https://developer.android.google.cn/topic/libraries/architecture/workmanager WorkManager codelab https://developer.android.google.cn/codelabs/android-workmanager#0
WorkManager 基础
上传日志或者报告数据
使用滤镜处理图片的同时保存图片
通过网络定期同步本地数据
// 获取最新的版本号 https://developer.android.google.cn/jetpack/androidx/releases/work
def work_version = "2.5.0"
implementation "androidx.work:work-runtime-ktx:$work_version"
首先实现我们自己的 Worker 类。我们会在这里实现真正需要在后台执行业务的代码。您可以扩展 Worker 类,并且复写 doWork() 方法。由于这个类非常重要,我们会在后边内容中进行详细介绍。这里是它最初的实现代码。
/* Copyright 2020 Google LLC.
SPDX-License-Identifier: Apache-2.0 */
class BlurWorker(ctx: Context, params: WorkerParameters) : Worker(ctx, params) {
override fun doWork(): Result {
val resourceUri = inputData.getString(KEY_IMAGE_URI)
return try {
if (resourceUri.isNullOrEmpty()) {
Timber.e("Invalid input uri")
throw IllegalArgumentException("Invalid input uri")
}
val outputData = blurAndWriteImageToFile(resourceUri)
Result.success(outputData)
} catch (throwable: Throwable) {
Timber.e(throwable, "Error applying blur")
Result.failure()
}
}
…
}
OneTimeWorkRequest.Builder https://developer.android.google.cn/reference/androidx/work/OneTimeWorkRequest.Builder
/* Copyright 2020 Google LLC.
SPDX-License-Identifier: Apache-2.0 */
val blurBuilder = OneTimeWorkRequestBuilder<BlurWorker>()
val data = workDataOf(KEY_IMAGE_URI to imageUri.toString())
blurBuilder.setInputData(data)
workDataOf https://developer.android.google.cn/reference/kotlin/androidx/work/package-summary#workdataof
/* Copyright 2020 Google LLC.
SPDX-License-Identifier: Apache-2.0 */
val workManager = WorkManager.getInstance(application)
val continuation = workManager.beginUniqueWork(blurBuilder.build())
// 执行任务
continuation.enqueue()
使 Worker 开始执行任务
当您使用 Worker 的时候,WorkManager 会在后台线程中自动调用 Worker.doWork()。doWork() 返回的 Result 会告知 WorkManager 服务是否成功,如果失败则告知是否需要重试。
Worker https://developer.android.google.cn/reference/androidx/work/Worker?hl=en
但是如果我希望进行异步操作怎么办?
一个简单的 BlurredImage 实体 一个用于插入和获取图片的 DAO 类 数据库
https://github.com/googlecodelabs/android-workmanager/pull/213
CoroutineWorker
https://developer.android.google.cn/reference/kotlin/androidx/work/CoroutineWorker
/* Copyright 2020 Google LLC.
SPDX-License-Identifier: Apache-2.0 */
class BlurWorker(ctx: Context, params: WorkerParameters) : CoroutineWorker(ctx, params) {
override suspend fun doWork(): Result {
val resourceUri = inputData.getString(KEY_IMAGE_URI)
return try {
if (resourceUri.isNullOrEmpty()) {
Timber.e("Invalid input uri")
throw IllegalArgumentException("Invalid input uri")
}
val outputData = blurAndWriteImageToFile(resourceUri)
// 将 uri 存储到数据库
val imageDao = ImagesDatabase.getDatabase(applicationContext).blurredImageDao()
imageDao.insert(BlurredImage(resourceUri))
Result.success(outputData)
} catch (throwable: Throwable) {
Timber.e(throwable, "Error applying blur")
Result.failure()
}
}
...
}
WorkManager 文档 https://developer.android.google.cn/topic/libraries/architecture/workmanager/ Codelab | 使用 WorkManager https://developer.android.google.cn/codelabs/android-workmanager#0 Codelab | WorkManager 进阶 https://developer.android.google.cn/codelabs/android-adv-workmanager#0
推荐阅读