Home > Back-end >  Does a worker from WorkManager in Android start to run even if the app has not been launched?
Does a worker from WorkManager in Android start to run even if the app has not been launched?

Time:04-20

I am following the Udacity Android Kotlin Developer course. In one of the lessons, the instructor taught about doing background tasks using WorkManager always to cache data in the background to show fresh data at the app's start.

So the code to start WorkManager refreshing data periodically is defined in the Main Application of the app.

class DevByteApplication : Application() {

/**
 * onCreate is called before the first screen is shown to the user.
 *
 * Use it to setup any background tasks, running expensive setup operations in a background
 * thread to avoid delaying app start.
 */
val applicationScope = CoroutineScope(Dispatchers.Default)

override fun onCreate() {
    super.onCreate()
    Timber.plant(Timber.DebugTree())
    delayedInit()
}

private fun delayedInit() = applicationScope.launch {
    setupRecurringWork()
}

private fun setupRecurringWork() {
    val constraints = Constraints.Builder()
        .setRequiredNetworkType(NetworkType.UNMETERED)
        .setRequiresBatteryNotLow(true)
        .setRequiresCharging(true)
        .apply {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                setRequiresDeviceIdle(true)
            }
        }.build()

    val repeatingRequest = PeriodicWorkRequestBuilder<RefreshDataWorker>(1, TimeUnit.DAYS)
        .build()

    WorkManager.getInstance().enqueueUniquePeriodicWork(
        RefreshDataWorker.WORK_NAME,
        ExistingPeriodicWorkPolicy.KEEP,
        repeatingRequest)
}}

Questions are:

So does WorkManager only start to work if an app is launched once? Or does it start to work once we install the application?

Also,

1. If we shut down the phone completely - will the WorkManager of our app work once we turn on the phone back 2. If we close the application completely - will the WorkManager still work?

If you have sources that talk specifically about these questions, I would love to read them!

CodePudding user response:

First question - the application must be launched at least once to schedule work.

There is no clear answer to the second question.

According to docs

Scheduled work is stored in an internally managed SQLite database and WorkManager takes care of ensuring that this work persists and is rescheduled across device reboots.

and

WorkManager is intended for work that is required to run reliably even if the user navigates off a screen, the app exits, or the device restarts.

But there may be some exceptions, see this answer for example.

CodePudding user response:

First question

The application must be launched at least once to schedule work, installing the application is not enough.

Second question

The phone needs to have completed the boot and been unlocked once. This because WorkManager is marked as not direct boot aware. You can read more about Direct Boot Mode on this guide.

Once the phone completed its boot, WorkManager will execute the scheduled work independently from having launched or not the application.

The only exception is for application that have been force stopped. In this case all notification and all scheduled Jobs (as WorkManager relies to Android's JobScheduler) are cancelled till the application is launched by the user.

Once the application is executed at least once, WorkManager will pick up all the Work and reschedule it.

Documentation

WorkManager documentation is quite good and covers all the API surface, some additional content is available on this blog series.
Videos about WorkManager are available on the Android Developer channel on youtube.

  • Related