WorkManager and Side Effects
WorkManager has a fairly clean and easy API and hides a lot of the complexity of scheduling background work that is not time-sensitive.
However, it has side effects.
To be able to restart your scheduled work after a reboot, WorkManager registers an ACTION_BOOT_COMPLETED receiver named androidx.work.impl.background.systemalarm.RescheduleReceiver. To be a good citizen, WorkManager only enables that receiver when you have relevant work and disables it otherwise. That way, your app does not unnecessarily slow down the boot process if there is no reason for your app to get control at boot time.
However, enabling and disabling a component, such as a receiver, triggers an ACTION_PACKAGE_CHANGED broadcast. Few apps directly have any code that watches for this broadcast, let alone would be harmed by having that broadcast be sent more times that might otherwise be necessary.
App widgets, though, are affected by ACTION_PACKAGE_CHANGED. Specifically, ACTION_PACKAGE_CHANGED triggers an onUpdate() call to your AppWidgetProvider.
That too may not be a problem for most app widgets. Ideally, your AppWidgetProvider makes no assumptions about when, or how frequently, it gets called with onUpdate(). However, there is one area where this is a problem: with an AppWidgetProvider scheduling work in onUpdate(). The flow then becomes:
- A “regular”
onUpdate()call comes in - Your
AppWidgetProviderschedules some work withWorkManager -
WorkManagerenablesRescheduleReceiver - That triggers
ACTION_PACKAGE_CHANGED, which triggers anonUpdate()call - Your
AppWidgetProviderschedules some work withWorkManageragain -
WorkManagereventually gets through those two pieces of work -
WorkManagerdisablesRescheduleReceiver, since it is no longer needed - That triggers
ACTION_PACKAGE_CHANGED, which triggers anonUpdate()call - Your
AppWidgetProviderschedules some work withWorkManager -
WorkManagerenablesRescheduleReceiver - And we’re in an infinite loop
The recommendation from Google is to avoid unconditionally scheduling work with WorkManager from onUpdate(). Instead, only do it if you know that the work is needed and that it is safe to do so, meaning that you will not get into the infinite loop.
That advice may be difficult for some to implement.
This, and any other possible side-effects of WorkManager, are not documented. So, you need to be a bit careful about your use of WorkManager:
- If your app responds to
ACTION_PACKAGE_CHANGEDbroadcasts, directly or indirectly, it may not be safe to schedule work there, lest you wind up in the infinite loop scenario described above. - If your app responds to
ACTION_BATTERY_OK,ACTION_BATTERY_LOW,ACTION_POWER_CONNECTED,ACTION_POWER_DISCONNECTED,ACTION_DEVICE_STORAGE_LOW,ACTION_DEVICE_STORAGE_OK,CONNECTIVITY_CHANGE,ACTION_TIME_SET, orACTION_TIMEZONE_CHANGED, bear in mind thatWorkManagerhas receivers for those broadcasts in your app. These are all disabled at the outset, but presumablyWorkManagerhas code to enable them based on certain conditions, such as certain constraints that you set in your work requests. Be careful about scheduling work withWorkManageron those broadcasts as well. - If your app responds to
ACTION_BOOT_COMPLETEDbroadcasts, bear in mind thatWorkManageralso depends on this broadcast. Your respective receivers might be invoked in any order. It may not be safe to schedule work here, asWorkManagermight assume that its ownACTION_BOOT_COMPLETEDreceiver has completed its work by the time you try scheduling new work. While I would not expect an infinite loop scenario, this is the sort of edge case that requires a lot of testing to ensure everything will work as expected. -
WorkManagerhas aContentProviderthat it bakes into your app as well. While scheduling your own work fromonCreate()of aContentProviderwould be rather odd, it’s possible that somebody might want to do that. Be careful, asWorkManagermay not be fully ready for operation at that point. Note that, last time I tested it, allContentProviderinstances are created beforeonCreate()ofApplication, so probably it is safe to schedule work there.
There may be other edge and corner cases beyond these. So, while WorkManager is nice, make sure that you thoroughly test your use of it.
Prev Table of Contents Next
This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.