我的当前Android应用采用了
archWorkerRuntimeVersion = '2.3.0-beta02'
api "androidx.work:work-runtime:$archWorkerRuntimeVersion"
api "androidx.work:work-runtime-ktx:$archWorkerRuntimeVersion"
处理后台任务。
当工作程序正在运行时,我会显示一个工具栏进度旋转器,向用户表示应用程序正在“忙碌”状态。
我的应用程序由多个活动组成,我使用 Android JetPack ViewModels 来管理工作者。
我按以下方式启动工作:
val myWorkRequest: OneTimeWorkRequest =
OneTimeWorkRequest.Builder(MyWorker::class.java)
.addTag(WORK_IN_PROGRESS_TAG + "${UUID.randomUUID()}").build()
WorkManager.getInstance(applicationContext)
.beginUniqueWork(
UNIQUE_WORK_NAME,
ExistingWorkPolicy.KEEP,
myWorkRequest
)
.enqueue()
我有一个定义如下的观察者:
private val observer = Observer<WorkInfo> { workInfo ->
when(workInfo.state) {
WorkInfo.State.RUNNING -> Log.e("OBSERVING", "${UUID.randomUUID()} :: show Progress Spinner")
WorkInfo.State.ENQUEUED -> Log.e("OBSERVING", "${UUID.randomUUID()} :: show Progress Spinner")
WorkInfo.State.SUCCEEDED -> Log.e("OBSERVING", "${UUID.randomUUID()} :: stop showing Progress Spinner")
WorkInfo.State.FAILED -> Log.e("OBSERVING", "${UUID.randomUUID()} :: stop showing Progress Spinner")
WorkInfo.State.BLOCKED -> Log.e("OBSERVING", "${UUID.randomUUID()} :: show Progress Spinner")
WorkInfo.State.CANCELLED -> Log.e("OBSERVING", "${UUID.randomUUID()} :: stop showing Progress Spinner")
}
}
我将如下观察工作流程: -
WorkManager.getInstance(applicationContext)
.getWorkInfoByIdLiveData(myWorkRequest.id)
.observe(lifeCycleOwner, observer)
并在我的视图模型中记住当前的workId
class MyViewModel : ViewModel() {
private var currentWorkInfoId: UUID? = null
fun startWorkTags(lifeCycleOwner: LifecycleOwner, applicationContext: Context) {
val myWorkRequest: OneTimeWorkRequest =
OneTimeWorkRequest.Builder(MyWorker::class.java)
.addTag(WORK_IN_PROGRESS_TAG + "${UUID.randomUUID()}").build()
WorkManager.getInstance(applicationContext)
.beginUniqueWork(
UNIQUE_WORK_NAME,
ExistingWorkPolicy.KEEP,
myWorkRequest
)
.enqueue()
currentWorkInfoId = myWorkRequest.id
}
}
我有如下问题:
1). 我正在使用
ExistingWorkPolicy.KEEP
,需要知道是否已经有后台工作在运行,以便我不会记错工作 ID。目前,我无条件地记住最后一个尝试执行 enqueue()
的工作 ID。有没有办法检测到 ExistingWorkPolicy.KEEP
没有启动新工作?2). 当用户退出我的应用程序时,我将当前工作 ID 存储在我的 viewModel 中,导致失去了这个 ID。当用户重新进入我的应用程序时,我该如何发现自己有活动的 worker?
对于问题 2)。我已尝试使用
WorkManager.getInstance(applicationContext)
.getWorkInfosForUniqueWorkLiveData(UNIQUE_WORK_NAME)
.observe(lifeCycleOwner, observerUnique)
然而,这种方法“看到”了所有先前的工人执行情况,我该如何识别最新的工人实例?