Mobile
Article

Optimizing Battery and Data Consumption in Android

By Valdio Veliu

Using Android Studio? Download our Handy Cheat Sheet of Keyboard Shortcuts!

Battery and network data consumption are two core issues developers have to deal with when developing mobile apps. This is more of a concern in mobile technologies because smartphones have limited resources. There are two key points I will be focusing on this article: battery life and network data reduction. I will go through some tips and examples on how to save battery life and keep network consumption at its minimum.

Optimizing battery life

The hardware components that consume most of the battery are the CPU, sensors, and the screen. Sensors include GPS, NFC, Bluetooth, etc.

Keeping this in mind is simple to determine what are some points for a developer to focus on while developing. Tasks like keeping the CPU utilization to a minimum required, minimizing the radio utilization and minimizing network operations are difficult to apply in many cases but are necessary to build a top of the line app.

In the following sections, I will go through a few factors that have an impact on battery drain. Also, I will mention some tips on how to avoid or reduce battery drain.

Carefully use Animations

It is obvious that animations need a lot of processing power by the CPU and therefore consume a significant amount of power. According to the documentation, most animations look fluid at 30 frames per second. So, going over 30 frames can be a waste of processing power and furthermore, more battery.

Another tip that comes from the documentation is to let the CPU sleep between animations. This is due to the fact that continuous animations lead to constant changes on the device screen. As I mentioned earlier the screen is one of the main factors of battery drain.

Image resources

Rendering large image bitmaps on screen also leads to high battery consumption. Displaying images on the screen cannot be avoided, but a tip here is not to use graphic elements that are larger than needed.

If Bitmap Assets have transparent pixels that can be cropped out, it’s a good idea to do so. Cropping out unnecessary pixels has an impact on performance and consumes less battery.

Carefully use WakeLocks

The tasks of an application are not only happening when the app is on the screen and the user is interacting with it. Some applications need to perform operations even when the phone is sleeping. This tasks cannot be completed when the phone is in sleep mode.

To perform tasks when the phone is sleeping the app needs to wake up the screen and to do so it needs to utilize WakeLocks.

WakeLocks are used to keep the screen and the CPU On. A usual example of WakeLocks is in mobile games and video playing apps when the screen stays ON even when the user is not interacting.
The problem with WakeLocks is that if not used properly can leave the device awake for a long time, therefore consuming battery and resources.
To use WakeLocks properly follow the guidelines of the Android Developers documentation.

Battery optimization strategies

For some noncrucial tasks of an application whose execution can be postponed at another time is a good strategy do so when the device is charging. This is usually the case for background tasks.
Delaying this kind of tasks when the device is charging will not have an influence on battery life. An example, in this case, can be data synchronization with some web service.

A nice solution for this kind of tasks comes from Google with the JobScheduler API. This API was created specifically for background tasks to schedule them efficiently to run together with other apps jobs. So really take it under consideration if your app has to perform heavy tasks.

Efficient Networking and Battery Life

What I have been pointing out till now are some of the factors that have an impact on battery life, but none of the mentioned factors consume more battery than network operations.

Networking is the most significant source of battery drain. So we need some way to cut the battery consumption that comes from this operation.

To clearly understand the impact that network operations have on battery consumption we must first understand the way networking operations are performed in terms of resource utilization. Network operations are performed through the so-called Radios, either WiFi or 3G/4G radios.

In order to conserve energy the Android system does not keep this radios always at full power, or the battery life would decrease rapidly. To demonstrate this, take a look at the different states of the 3G wireless radio.

Radio States

  • Full power – network radio is active, some network operations are performed.
  • Low power – intermediate state that uses 50% of the battery power.
  • Standby – no network operations, low battery consumption.

When a device has no network tasks to perform the radio state machine is transitioned on a low and idle state to consume significantly less battery. When a network connection is created, the system transits the radio to the full power state. According to the Android Developers documentation, the radio will keep its Full Power state for the whole time it’s utilized, plus an additional 5 seconds tail time, followed by 12 seconds of Low Energy state before it goes to Standby.

For one network operation that is performed, the device is losing battery power for 18 seconds, if we consider the network operation is performed during 1 second, which is true in most cases. If you think about it there is a lot of power being wasted in one single network operation.

In the following sections of this article, I will provide some tips and smart solutions to avoid overusing the battery resources when performing network operations.

Batch Connections

As noticed before, a single network task consumes about 18 seconds of battery ineffectively.
What if all the network connections are grouped together and performed one after the other? Obviously, in this case, the radio won’t go through the whole process of waking up and sleeping in every network call. It will stay active until all the networking is done and then it will go in its Standby state.

If we do a simple math, the 18-second process in every operation is accumulated in one big network operation. In overall the radio stays active for a shorter time than having separated tasks.

The problem here is that tasks have to be grouped together and performed at the same Full Power window. The thing is that not all requests can wait until a group is formed. This solution is more efficient for background tasks and the JobScheduler API can help with this jobs. The JobScheduler API groups tasks from many apps for far better resource utilization.

Prefetch Data

In most cases, it’s hard to predict what the user will do in apps and what data to load. If possible it’s a good idea to prefetch as much data as possible to avoid unnecessary requests.
For example, if you are developing an audio player application and have caching capabilities for the sounds of the app, it is a good idea to preload the next track on the list, because is highly likely that the user will play the next track.

Ads are a factor in battery drain

Mobile ads are one of the primary revenue providers for many apps, but ads are a big factor in battery drain. Ads use processing power to display colorful images and even videos but also consume network. Ads utilize a lot of bandwidth loading large content resulting in more battery drain.

I am not asking for ads to be removed in mobile apps, because ads are one of the most common ways developers gain revenue, but try not to exaggerate with ads. A nice solution for both the battery and earning revenue from ads is to limit ads to only a few places in your apps.

Analyzing battery with Monitoring tools

To have a better understanding of the battery consumption you need a way to monitor the app’s stats. Android Studio has a built-in monitoring tool for reviewing the resources an app is consuming.

Based on the networking strategies mentioned earlier you can determine the impact that networking has on battery. The following images show what a good and a bad networking strategy look like in terms of network tasks.

Good example – network tasks are grouped
Good example

Bad example
Bad example

The network bursts in the second image are a clear example of bad networking strategy.

Battery historian

Battery historian is another great tool provided by Google that provides battery analysis by using Android bug report files. It provides a detailed overview of the battery statistics of Android devices by showing which processes are draining battery.
The following chart is an example of what this tool provides in terms of battery monitoring.

battery-historian/

Battery Historian is open-sourced and available on GitHub.

Wrapping Up

If you are building a big application that requires a lot of resources, battery optimization should be one of the key optimization issues to keep in mind.

I hope the tips and guidelines provided in this article provide a good insight on what to focus on in the context of network operations and battery optimization.

More:

No Reader comments

Recommended
Sponsors
Get the latest in Mobile, once a week, for free.