-
Notifications
You must be signed in to change notification settings - Fork 2
Workmanager
Devrath edited this page Jun 5, 2021
·
15 revisions
-
Work-Manager
is a new library fromandroid architecture components
- It simplifies the work of getting the background tasks done, even if the application gets killed. This is very hard to achieve in the latest android versions using the old android
API's
. - It adheres to power-saving features like doze-mode.
- It is backward compatible.
-
Work-Manager
is a clever mobile assistant that helps to perform heavy tasks in the background thread. - It is in such a way that, we have certain tasks to complete, we hand over the tasks to the assistant stating the conditions in with the task has to be done. And finally, the assistant takes care of the task getting done.
- For example, You can tell the assistant work-manager to
perform a task every one hour
,not to start the work until the device has network
, `not to start the work until the battery is low. -
Work-manager
continues the work even if the app is in the background or the OS kills the app. - Summarizing the work-manager could intelligently use the defined factors and get the work done for you.
-
Tasks that don't need to be executed instantly need to be executed in the guarantee that is best suited for the work manager.
-
Should you need to hand over the task of performing the network request, parsing the retrieved data, and showing inactivity
->
No because that can be done in an instant and can be canceled when the application process is removed. -
What about the task of sending the logs to the server, processing and syncing the data, upload a large no of videos and images, and others
->
Yes because these might not have to be done instantly.
-
Should you need to hand over the task of performing the network request, parsing the retrieved data, and showing inactivity
Classes Names | Description |
---|---|
WorkManager | This is the main class to enqueue the work requests |
Worker | This is a sub-class that does the background task |
WorkRequest | This defines the background task |
WorkStatus | Running, Enqueued, Finished etc. |
- We can have a policy in such a way that there will be a unique instance of work manager
- We can use a work policy for this
- We have certain options.
-
Replace
- > If there exists a work manager with a unique tag we can replace it with a new one. -
Append
- > If there exists a work manager with a unique tag we can append the existing one with a new one. -
Keep
- > Ignore the new one and keep the already running one keep running.
-
- we can listen to work manager changes using live data and if required we can cancel it
- We can find the work manager instance using below options
cancelUniqueWork()
cancelAllWorkByTag()
cancelWorkById()