Background Work with Android WorkManager
What is WorkManager?
WorkManager is an Android library that supports asynchronous one-off and periodic tasks as well as managing network conditions, storage space and charging status.
It allows the chaining of complex work requests, including running work in parallel, so we can schedule the order that each action is performed, even when the app has been closed.
WorkManager is part of "Android Jetpack" which is an "Architecture Component" for "Background Processing" tasks that need to be carried out even when your users navigate away from your app (e.g. syncing data with a cloud server).
WorkManager sits on top of a few long-existing Android APIs such as “JobScheduler” and “AlarmManager” and it picks the best APIs to use depending on the version of Android installed on the user's Android device.
Jetpack is a collection of Android software components which make it faster for us to develop cutting-edge Android apps (the quicker we develop your bespoke Android apps the cheaper the price as we base the cost of our bespoke Android app development on the number of developer hours used to code and test your Android app). These components help us follow best practices, whilst simplifying complex tasks, so we can focus on coding the algorithms and business logic for your specific business requirements.
What are Android Architecture Components?
Android architecture components are a collection of software libraries that help us develop robust, scalable, testable, and maintainable bespoke Android apps. These include classes for managing the User Interface (UI) component lifecycle as well as handling data persistence whilst avoiding memory leaks.
What is the Android JobScheduler?
This is an Android API for scheduling various types of jobs which will be executed in your app's own process. The Android framework will be intelligent about when it executes jobs and will attempt to batch and defer them as much as possible. If we don't specify a deadline for a job, it will be run at any moment depending on the JobScheduler's internal queue. While a job is running the device stays awake for the duration of the job.
What is the Android AlarmManager?
The Android AlarmManager provides access to the system alarm services on an Android device which allows us to schedule your app to be run at some point in the future. When an alarm goes off we can automatically start your app if it is not already running. The AlarmManager is intended for cases where you want to have your application code run at a specific time, even if your application is not currently running.
What is Background Processing?
Every Android app has a main thread which is in charge of handling the UI (User Interface - i.e. the screen the user sees). If there is too much work happening on this thread, the app appears to hang or slow down, leading to a poor experience for your Android app's users. Any long-running computations and operations (e.g. syncing data with your cloud server) should be done on a separate background thread. In general, anything that takes more than a few milliseconds should be delegated to a background thread.
In order to maximise battery life, Android restricts background work when the app isn’t visible to the user.
- Android 6.0 introduced Doze mode and app standby. Doze mode restricts app behaviour when the screen is off and the device is stationary. App standby puts unused applications into a special state that restricts their network access, jobs, and syncs.
- Android 7.0 limited implicit broadcasts and introduced Doze-on-the-Go.
- Android 8.0 further limited background behaviour, such as getting location in the background and releasing cached wakelocks.
WorkManager is designed to give the best possible behaviour under these restrictions.