首页 > android > WorkManager - 永久删除定期工作人员

WorkManager - 永久删除定期工作人员 (WorkManager - remove periodic worker for good)

问题

我有一个更新,其中包含以下代码,每三个小时添加一个新的定期工作者。

fun runCouponValidatorWorker() {
    val constraints = Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build()
    val worker = PeriodicWorkRequestBuilder<CouponValidatorWorker>(3, TimeUnit.HOURS).setConstraints(constraints).build()
    WorkManager.getInstance()?.enqueueUniquePeriodicWork("couponValidatorWorker", ExistingPeriodicWorkPolicy.REPLACE, worker)
}

我想发布一个更新,它将“杀死”这个worker以及这个worker的每个计划实例。

最好的方法是什么?

解决方法

你试过简单地叫这个:

WorkManager.getInstance()?.cancelUniqueWork("couponValidatorWorker")

问题

I had an update with the following code that adds a new periodic worker every three hours.

fun runCouponValidatorWorker() {
    val constraints = Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build()
    val worker = PeriodicWorkRequestBuilder<CouponValidatorWorker>(3, TimeUnit.HOURS).setConstraints(constraints).build()
    WorkManager.getInstance()?.enqueueUniquePeriodicWork("couponValidatorWorker", ExistingPeriodicWorkPolicy.REPLACE, worker)
}

I'd like to release an update that will "kill" this worker and every scheduled instance of this worker.

What would be the best way to do that?

解决方法

Did you try simply calling this:

WorkManager.getInstance()?.cancelUniqueWork("couponValidatorWorker")
相似信息