Starting a background task using WorkManager
Historically, Google offered Android developers multiple ways of executing such tasks: services, JobScheduler, and Firebase’s JobDispatcher and AlarmManager. With the fragmentation that existed in the Android world, it was quite a mess to cope with. Luckily for us, since March 2019, we have had a better (more stable) option.
With the introduction of WorkManager, Google has abstracted the logic of choosing a background execution mechanism based on the API version away from us. WorkManager works on Android, going all the way back to API 14 (Android 4.0, or Ice Cream Sandwich). We still use a foreground service, which is a special kind of service, for certain tasks that should be known to the user while running, such as playing music or tracking the user’s location in a running app.
The first question we will address in this section is as follows: Should we opt for WorkManager or a foreground service? To answer that,...