Optimizing Battery and Data Consumption in Android

Share this article

Optimizing Battery and Data Consumption in Android

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.

Frequently Asked Questions (FAQs) on Optimizing Battery and Data Consumption in Android

What are the main causes of high battery and data consumption in Android apps?

The main causes of high battery and data consumption in Android apps include frequent network requests, inefficient use of resources, unnecessary background processes, and excessive use of hardware components like GPS. These activities can drain the battery quickly and consume a lot of data, leading to a poor user experience.

How can I optimize network requests to reduce battery and data consumption?

Network requests can be optimized by batching them together instead of sending them individually. This reduces the number of times the device has to connect to the network, saving both battery and data. Additionally, using a more efficient data format like Protocol Buffers instead of JSON can also help reduce data consumption.

How does the use of resources affect battery and data consumption?

Inefficient use of resources can lead to high battery and data consumption. For example, keeping unnecessary services running in the background can drain the battery quickly. Similarly, loading large images or files can consume a lot of data. Therefore, it’s important to manage resources efficiently to optimize battery and data consumption.

How can I prevent unnecessary background processes?

Unnecessary background processes can be prevented by using services judiciously. Services should be stopped when they are not needed. Also, using JobScheduler or WorkManager can help schedule tasks efficiently, reducing the need for constant background processes.

How does the use of hardware components affect battery consumption?

Excessive use of hardware components like GPS, camera, and sensors can drain the battery quickly. Therefore, these components should be used sparingly and turned off when not in use. Also, using lower power modes can help save battery.

How can I monitor the battery and data consumption of my app?

Android provides several tools to monitor the battery and data consumption of your app. The Battery Historian tool can help analyze the battery usage, while the Data Usage section in the device settings can provide information about data consumption.

How can I test the battery and data consumption of my app?

You can test the battery and data consumption of your app using the Android Profiler in Android Studio. This tool provides real-time data about CPU, memory, and network usage, helping you identify areas of your app that need optimization.

How can I optimize the use of GPS to save battery?

GPS can be optimized by requesting location updates less frequently or by using a lower accuracy mode. Also, location updates should be stopped when they are not needed.

How can I reduce data consumption when loading images?

Data consumption can be reduced when loading images by using efficient image loading libraries like Glide or Picasso. These libraries can help resize images, cache them, and load them asynchronously, reducing data consumption.

How can I optimize the use of sensors to save battery?

Sensors can be optimized by using them only when necessary and turning them off when not in use. Also, using sensor batching can help reduce battery consumption.

Valdio VeliuValdio Veliu
View Author

Valdio recently graduated in Computer Engineering. He is a mobile developer, who is passionate about mobile technologies and learning new things. He has worked with languages such as C, Java, php and is currently focused on Java, mobile and web development

androidArielEbattery
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week