PeriodicWorkRequest 无论如何都不起作用
PeriodicWorkRequest Does not Work No Matter What
PeriodicWorkRequest 根本不起作用。如您所见,我已将重复间隔设置为至少 15 分钟。它被排队,然后显示 运行 但仅此而已。什么都没发生,我什至等了 30 分钟!这是行不通的。然而,OneTimeWorkRequest 工作得很好,没有任何问题,但在涉及到 PeriodicWorkRequest 时就没有运气了。请看下面我的代码:
private fun processOneTimeADayNotifyReq() {
val oneTimeADayReq: PeriodicWorkRequest = PeriodicWorkRequest.Builder(StepCountNotificationWorker::class.java, PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS
, TimeUnit.MINUTES)
.build()
val workManager: WorkManager = WorkManager.getInstance(this.context)
workManager.enqueue(oneTimeADayReq)
this.workerLiveData = workManager.getWorkInfoByIdLiveData(oneTimeADayReq.id)
this.workerLiveData!!.observeForever(observeStepWorkerLiveData())
}
private fun observeStepWorkerLiveData(): Observer<WorkInfo> {
return Observer {
if (it?.state == null) {
return@Observer
} else {
when (it.state) {
WorkInfo.State.RUNNING -> {
Log.i("Running: ", "running")
}
WorkInfo.State.BLOCKED -> {
Log.i("Blocked: ", "blocked")
}
WorkInfo.State.CANCELLED -> {
Log.i("Cancled: ", "canceled")
}
WorkInfo.State.ENQUEUED -> {
Log.i("Enqueued:", "enqueued")
}
WorkInfo.State.SUCCEEDED -> {
val outputData:Data = it.outputData
val calories: Int = outputData.getInt(Constants.STEP_COUNT_VALUE, 0)
Log.i("Calories: ", calories.toString())
}
WorkInfo.State.FAILED -> {
}
else -> {
return@Observer
}
}
}
}
}
我也试过设置弹性间隔:
val oneTimeADayReq: PeriodicWorkRequest = PeriodicWorkRequest.Builder(StepCountNotificationWorker::class.java, PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS
, TimeUnit.MINUTES, PeriodicWorkRequest.MIN_PERIODIC_FLEX_MILLIS, TimeUnit.MINUTES)
.build()
根本行不通!我试过同时使用 2.3.0-alpha01 和 2.3.0-beta01 版本。没有运气。我正在使用 Google Pixel 3a,targetSdkVersion 是 29。有人可以帮忙吗?谢谢你。
您正在将重复间隔设置为 900000 分钟,要将其设置为 15 分钟,如果您使用 PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLI
则使用 TimeUnit.MILLISECONDS
:
val oneTimeADayReq: PeriodicWorkRequest = PeriodicWorkRequest.Builder(
StepCountNotificationWorker::class.java,
PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS,
TimeUnit.MILLISECONDS).build()
执行结束时从RUNNING
到ENQUEUED
的周期性工作转换,唯一的最终状态是CANCELLED
。
PeriodicWorkRequest 根本不起作用。如您所见,我已将重复间隔设置为至少 15 分钟。它被排队,然后显示 运行 但仅此而已。什么都没发生,我什至等了 30 分钟!这是行不通的。然而,OneTimeWorkRequest 工作得很好,没有任何问题,但在涉及到 PeriodicWorkRequest 时就没有运气了。请看下面我的代码:
private fun processOneTimeADayNotifyReq() {
val oneTimeADayReq: PeriodicWorkRequest = PeriodicWorkRequest.Builder(StepCountNotificationWorker::class.java, PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS
, TimeUnit.MINUTES)
.build()
val workManager: WorkManager = WorkManager.getInstance(this.context)
workManager.enqueue(oneTimeADayReq)
this.workerLiveData = workManager.getWorkInfoByIdLiveData(oneTimeADayReq.id)
this.workerLiveData!!.observeForever(observeStepWorkerLiveData())
}
private fun observeStepWorkerLiveData(): Observer<WorkInfo> {
return Observer {
if (it?.state == null) {
return@Observer
} else {
when (it.state) {
WorkInfo.State.RUNNING -> {
Log.i("Running: ", "running")
}
WorkInfo.State.BLOCKED -> {
Log.i("Blocked: ", "blocked")
}
WorkInfo.State.CANCELLED -> {
Log.i("Cancled: ", "canceled")
}
WorkInfo.State.ENQUEUED -> {
Log.i("Enqueued:", "enqueued")
}
WorkInfo.State.SUCCEEDED -> {
val outputData:Data = it.outputData
val calories: Int = outputData.getInt(Constants.STEP_COUNT_VALUE, 0)
Log.i("Calories: ", calories.toString())
}
WorkInfo.State.FAILED -> {
}
else -> {
return@Observer
}
}
}
}
}
我也试过设置弹性间隔:
val oneTimeADayReq: PeriodicWorkRequest = PeriodicWorkRequest.Builder(StepCountNotificationWorker::class.java, PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS
, TimeUnit.MINUTES, PeriodicWorkRequest.MIN_PERIODIC_FLEX_MILLIS, TimeUnit.MINUTES)
.build()
根本行不通!我试过同时使用 2.3.0-alpha01 和 2.3.0-beta01 版本。没有运气。我正在使用 Google Pixel 3a,targetSdkVersion 是 29。有人可以帮忙吗?谢谢你。
您正在将重复间隔设置为 900000 分钟,要将其设置为 15 分钟,如果您使用 PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLI
则使用 TimeUnit.MILLISECONDS
:
val oneTimeADayReq: PeriodicWorkRequest = PeriodicWorkRequest.Builder(
StepCountNotificationWorker::class.java,
PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS,
TimeUnit.MILLISECONDS).build()
执行结束时从RUNNING
到ENQUEUED
的周期性工作转换,唯一的最终状态是CANCELLED
。