5

This question has been asked many times in different forms but I am not getting it right and are wondering if it might not be an android bug.

After setting up WorkManager doWork() is called at random times and sometimes up to 10+ times in quick succession.

  • It is called only once in my MainApplication (*see point below)
  • If it is called unexpectedly I try clearing and restarting it
  • I have uninstalled and re-installed the App
  • I clear all instances prior to calling WorkManager
  • I have tried creating it with a unique tag

The expected behaviour is a call once every 10-15 minutes

Please see details below: Implemented on LG K22 using Android studio Bumblebee 2021.1.1 Patch 2

Gradle Implementation:

implementation 'androidx.work:work-runtime-ktx:2.7.1'

MainActivity Code:

    WorkManager.getInstance(this).cancelAllWorkByTag("scanDevices")
    val myMonitorWorkerRequest = PeriodicWorkRequest.Builder(myMonitorWorker::class.java,
        15, TimeUnit.MINUTES,5, TimeUnit.MINUTES).build()

    WorkManager.getInstance(this)
        .enqueueUniquePeriodicWork(
            "scanDevices",
            ExistingPeriodicWorkPolicy.KEEP,
            myMonitorWorkerRequest)
  • note: ExistingPeriodicWorkPolicy.REPLACE does not change the behavior here

Original Worker Class:

  override fun doWork(): Result {
    try{
        runNotificationService()
        return Result.success()
    }
    catch (e:Exception){
        return Result.failure()
    }
}

Current Worker Class:

  override fun doWork(): Result {
    try{
        val timeNow = System.currentTimeMillis()
        if(timeNow < lastTime+600000){
            WorkManager.getInstance(theContext).cancelAllWorkByTag("scanDevices")
            val myMonitorWorkerRequest = PeriodicWorkRequest.Builder(myMonitorWorker::class.java,
                15, TimeUnit.MINUTES,5, TimeUnit.MINUTES).build()

            WorkManager.getInstance(theContext)
                .enqueueUniquePeriodicWork(
                    "scanDevices",
                    ExistingPeriodicWorkPolicy.KEEP,
                    myMonitorWorkerRequest)
        }
        else{
            runNotificationService()
            lastTime = timeNow
        }
        return Result.success()
    }
    catch (e:Exception){
        return Result.failure()
    }
}

The above code is not bulletproof yet since the worker class is called in such rapid succession sometimes, that multiple (instances / threads (I think)) are started at the same time and the updating of the global variables had not taken effect yet prior to another instance starting. It does dramatically reduce the number of calls though to only one or two additional instances slipping through.

Specifically, my question is, does anyone know if anything I am doing is either wrong or alternatively if there is a known bug report for multiple worker instances/threads being created?

See these similar references that I have tried but did not work:

WorkManager executing enqueueUniquePeriodicWork() multiple times at once?

doWork() for WorkManager called multiple times for OneTimeWorkRequest

*App Inspection shows 20 instances enqueued on android studio

Assay
  • 96
  • 7
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Mar 18 '22 at 16:02
  • Classic Googlers. Like can't they make something work properly? For once? Argh...100s of bugs in their android framework. Every damn day I bump into at least one. EVERY SINGLE DAY, I swear. Cmmon Google, wake up!!! – qkx Jul 05 '22 at 10:48
  • Same Problem for me.. What is your Android Version @Assay? Unfortunately mine is Android 5 (pretty old, i know). Hopefully that is fixed in newer Android Versions. – Caelis Aug 17 '22 at 11:34
  • @Caelis, the Android version on the phone is 10 – Assay Aug 18 '22 at 08:06

1 Answers1

2

I don't know the exact reason for this, but i faced the same problem today and it was very frustrating. So i decided to uninstall the application and it magically worked. Hope this is the case for you as well

  • 1
    I have previously tried re-installing but unfortunately that did not work for me. – Assay Jun 14 '22 at 04:06
  • Surprisingly this is what worked for me on my Emulator, I uninstalled the app it stopped firing every 15 Minutes. Thanks lots Haitham – Tonnie Feb 27 '23 at 15:59