Battery life is the single most important aspect of the mobile user experience. A device without power offers no functionality at all. For this reason, it is critically important that apps be as respectful of battery life as possible.
There are three important things to keep in mind in keeping your app power-thrifty:
- Make your apps Lazy First.
- Take advantage of platform features that can help manage your app's battery consumption.
- Use tools that can help you identify battery-draining culprits.
Making your app Lazy First means looking for ways to reduce and optimize operations that are particularly battery-intensive. The core questions underpinning Lazy First design are:
- Reduce: Are there redundant operations your app can cut out? For example, can it cache downloaded data instead of repeatedly waking up the radio to re-download the data?
- Defer: Does an app need to perform an action right away? For example, can it wait until the device is charging before it backs data up to the cloud?
- Coalesce: Can work be batched, instead of putting the device into an active state many times? For example, is it really necessary for several dozen apps to each turn on the radio at separate times to send their messages? Can the messages instead be transmitted during a single awakening of the radio?
You should ask these questions when it comes to using the CPU, the radio, and the screen. Lazy First design is often a good way to tame these battery killers.
To help you achieve these and other efficiencies, the Android platform provides a number of features to help maximize battery life.
Broadly speaking, the Android platform provides two categories of help for you to optimize your app's battery use. First, it provides several APIs that you can implement in your app. You can learn more about these APIs in Guide to background processing.
There are also internal mechanisms in the platform to help conserve battery life. While they are not APIs that you implement programmatically, you should still be aware of them so that your app can leverage them successfully. For more information, see:
- Doze and App Standby
- App Standby Buckets. The system limits apps' access to device resources like the CPU or battery, based on the user's usage patterns.
- Background restrictions. If an app exhibits bad behaviors, the system prompts the user to restrict that app's access to system resources.
- Power management restrictions. See a list of power restrictions that can be imposed on apps under certain conditions.
- Testing and troubleshooting
Additionally, Android 9 (API level 28) makes a number of improvements to battery saver mode. Device manufacturers determine the precise restrictions imposed. As an example, on AOSP builds, the system applies the following restrictions:
- The system puts apps in app standby mode more aggressively, instead of waiting for the app to be idle.
- Background execution limits apply to all apps, regardless of their target API level.
- Location services may be disabled when the screen is off.
- Background apps do not have network access.
See full details of the device-specific power optimizations at Power management restrictions.
As always, it's a good idea to test your app while battery saver is active. You can turn on battery saver manually through the device's Settings > Battery Saver screen.
You can get even more benefit out of these features by using the tools available for the platform to discover the parts of your app that consume the most power. Finding what to target is a big step toward successful optimization.
There are tools for Android, including Profile GPU Rendering and Battery Historian to help you identify areas that you can optimize for better battery life. Take advantage of these tools to target areas where you can apply the principles of Lazy First.