Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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.

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.

@pfmagii Wow! You are right! How in the world I missed that lol! Okay, I will test this and let you know! Thank you so much :))) – Junia Montana Nov 25, 2019 at 18:48

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.