Reputation: 623
PeriodicWorkRequest doesn't work at all. I have set the repeat interval to minimum 15 minutes as you can see. It gets enqueued, then shows running but then that's it. Nothing happens, I've even waited 30 minutes! It's just doesn't work. However, OneTimeWorkRequest works perfectly fine without any issue, but no luck when it comes to PeriodicWorkRequest. Please take a look at my code below:
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
}
}
}
}
}
I have also tried setting the flex interval:
val oneTimeADayReq: PeriodicWorkRequest = PeriodicWorkRequest.Builder(StepCountNotificationWorker::class.java, PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS
, TimeUnit.MINUTES, PeriodicWorkRequest.MIN_PERIODIC_FLEX_MILLIS, TimeUnit.MINUTES)
.build()
It just doesn't work! I've tried using both version 2.3.0-alpha01 and 2.3.0-beta01. No luck. I'm using Google Pixel 3a, targetSdkVersion is 29. Can anybody please help? Thank you.
Upvotes: 0
Views: 964
Reputation: 6476
You are setting the repetition interval to 900000 minutes, to set it 15 minutes, if you use PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLI
then use TimeUnit.MILLISECONDS
:
val oneTimeADayReq: PeriodicWorkRequest = PeriodicWorkRequest.Builder(
StepCountNotificationWorker::class.java,
PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS,
TimeUnit.MILLISECONDS).build()
A periodic work transition from RUNNING
to ENQUEUED
at the end of its execution, the only final state is CANCELLED
.
Upvotes: 2