0

Starting from Android 6.0 (API level 23), Android introduced Doze Mode and introduced various restrictions on background processing in Android. Android also introduced WorkManager API which makes it easy to specify asynchronous tasks and when they should run. From the Android documentation of WorkManager, we get :

Note: WorkManager is intended for tasks that require a guarantee that the system will run them even if the app exits.

I tested the same using the following program :

In MainActivity(Launcher activity of App), I create PeriodicWorkRequest with periodic interval of 15 mins( minimum allowed interval for periodic request) and handle it to workManager

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    runWorkRequest()

}

// create a PeriodicWorkRequest with Periodic Interval of 15 mins
fun runWorkRequest() {
    val periodicWorkRequestBuilder = PeriodicWorkRequest.Builder(BatteryWorker::class.java, PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS
            , TimeUnit.MILLISECONDS)
    val periodicWorkRequest = periodicWorkRequestBuilder.build()
    WorkManager.getInstance().enqueue(periodicWorkRequest)
}
}

and BackgroundWorker is as follows and logs whenever it is run :

public class BackgroundWorker extends Worker {
    @NonNull
    @Override
    public Result doWork() {

        Log.d("MyBackgroundWorker", "BackgroundWorker is Running");
        return Result.SUCCESS;
    }
}

Now if I run the above code on Api Level 28 and swiping the app off the Recent App List and turn the device screen off , Logs are printed periodically for just 40-45 mins and then the background worker is stopped. This is contrary to guaranteed execution that the documentation claims .

Does anyone has the solution for guaranteed indefinite periodic execution ( with periodic interval of 15 mins) in background in Android Oreo and above ?

Abhishek Luthra
  • 2,575
  • 1
  • 18
  • 34
  • Try a JobScheduler? – TheWanderer Sep 24 '18 at 14:26
  • From the android documentation of WorkManager(https://developer.android.com/topic/libraries/architecture/workmanager/) , we have : If your app is not running, WorkManager chooses an appropriate way to schedule a background task--depending on the device API level and included dependencies, WorkManager might use JobScheduler, Firebase JobDispatcher, or AlarmManager. You don't need to write device logic to figure out what capabilities the device has and choose an appropriate API; instead, you can just hand your task off to WorkManager and let it choose the best option. – Abhishek Luthra Sep 24 '18 at 14:32
  • From the source code of WorkManager , we further have : * WorkManager uses an underlying job dispatching service when available based on the following * criteria: *

      *
    • Uses JobScheduler for API 23+ *
    • For API 14-22 *
        *
      • If using Firebase JobDispatcher in the app and the optional Firebase dependency, uses * Firebase JobDispatcher *
      • Otherwise, uses a custom AlarmManager + BroadcastReceiver implementation *
    – Abhishek Luthra Sep 24 '18 at 14:35
  • So, Internally WorkManager is using JobScheduler API as I am testing on Android Api 28 which is surely greater than Api level 23 . – Abhishek Luthra Sep 24 '18 at 14:36
  • Sounds like you need the user to whitelist your app from Doze. What you want isn't really supported if they haven't. – Gabe Sechan Sep 24 '18 at 16:59

0 Answers0