Posted by Luiz Gustavo Martins, Accomplice Developer Advocate, Accomplice DevRel
That is the third in a collection of weblog posts during which define methods and steerage in Android with regard to energy.
Over time, executing background duties on Android has advanced. To write modern apps, it is essential to discover ways to run your background duties in fashionable trend.
When is an app within the background?
Earlier than understanding what background execution is, we have to have a transparent view of when Android understands an app to be within the foreground. An app is taken into account to be within the foreground if any of the next is true:
- The app has a visual exercise, whether or not the exercise is began or paused.
- The app has a foreground service.
- One other foreground app is related to the app, both by binding to one of its services, or utilizing one in all its content providers. For instance, an app is within the foreground if one other app or the system binds to its:
If none of these circumstances is true, the app is taken into account to be within the background.
Background execution adjustments
Working duties within the background consumes a tool’s restricted sources, like RAM and battery. This may end in a foul consumer expertise. For instance, background duties could degrade the battery lifetime of the system or the consumer could expertise poor system efficiency at occasions comparable to watching a video, taking part in a recreation, utilizing the digital camera.
To enhance battery life and provides a greater consumer expertise, Android has advanced over a number of releases to ascertain limits on background execution. These limits embrace:
- Doze and App Standby, which restricts app conduct when the display is off, and the system is idle and never charging.
- Background Location restrictions, limits how incessantly background apps can retrieve the consumer’s present location.
- Background Service Limits, that limit background providers from being run and consuming CPU/community in a hidden/non-visible approach.
- Most just lately, App Standby Buckets that may restrict the system sources out there to apps that are not utilized by customers, and App Restrictions the place the system will immediate the consumer to limit the app’s entry to system sources within the background if the app displays dangerous behaviors, and a number of other Battery Saver improvements.
Use circumstances and options
Deciding which instruments to make use of to implement background execution requires the developer to have a transparent understanding of what they need to accomplish, and beneath which restrictions. This flowchart might help you decide:
- WorkManager is the advisable resolution for background execution, considering all OS background execution limits. If it’s essential to assure job will run even whether it is deferred, it’s best to use WorkManager. This API permits you to schedule jobs (one-off or repeating) and chain and combine jobs. You may as well apply execution constraints to them comparable to triggering when the system is idle or charging, or executing when a content material supplier adjustments.
One instance is that if it’s essential to compress logs to add them to your server. To do that you may create two work requests:
- First: compress the file. On this step chances are you’ll add the constraint that the system must be charging.
- Second: add it to the server. For this request it’s best to add a community connectivity constraint in order that the work solely will get triggered when you could have a sound connection.
After enqueuing each duties, WorkManager will care for executing them when your app has entry to the sources you want.
One other good characteristic of WorkManager is that it respects power-management options, in order that if a job is scheduled to run at an outlined time and the system is in Doze at the moment, WorkManager will attempt to run the duty throughout a maintenance window if the constraints are met or after Doze is lifted.
- If a long-running job is to be scheduled in response to an exterior occasion like syncing for brand spanking new on-line content material, use Firebase Cloud Messaging to inform your app after which create a piece request with WorkManager to sync the content material. You may be taught extra about this in “Notifying your users with FCM“.
- If the app wants to finish a user-initiated job with out deferring even when the consumer leaves the app or turns off the display, comparable to within the case of music/video playback or navigation, it’s best to use a Foreground service. (The subsequent weblog publish on this collection dives deeper into this use case.)
- If it’s essential to run a job at an precise time that triggers actions, includes consumer interactions, and can’t be deferred, use AlarmManager (extra particularly the tactic setExactAndAllowWhileIdle). Examples of time alarms embrace:
- a reminder to take drugs
- a notification TV present is about to start out.
When the alarm is triggered, you could have only a few seconds to complete the work and your app could not have entry to the community (for instance throughout Doze or attributable to App Standby buckets). If you actually need community or to do an extended job, use WorkManager. Each time a wakeup alarm is triggered, the system comes out of low-power mode and holds a partial wake lock which may considerably impression the battery life over time. This may be monitored through extreme wakeups stats highlighted on Android Vitals, supplied through Google Play Console.
|Assured execution of deferrable work||
|A job initiated in response to an exterior occasion||
||FCM + WorkManager|
|Proceed user-initiated work that should run instantly even when the consumer leaves the app||
|Set off actions that contain consumer interactions, like notifications at an actual time.||
Use background execution judiciously to be able to construct cool apps that delight customers whereas saving their battery. In the event you want extra info on executing background duties on Android, there’s nice content material on the Android developer site.
Word: WorkManager remains to be in public preview. In the event you want another resolution proper now, it’s best to use JobScheduler, though it has limitations that do not apply to WorkManager. JobScheduler is a part of the Android Framework, and solely out there for Android API 21 and above; WorkManager works on API 14 and above.
Acknowledgements: This collection of weblog posts is produced in collaboration between the Android Framework and DevRel groups