Optimizing App-Initiated Network Use
This lesson teaches you to
Network traffic initiated by your app can usually be significantly optimized, since you can plan for what network resources it needs and set a schedule for accessing them. By applying careful scheduling, you can create significant periods of rest for the device radio and, thereby, save power. There are several Android APIs that can help with network access scheduling, and some of these functions can coordinate network access for other apps, further optimizing battery performance.
This lesson teaches you how to reduce battery consumption by applying techniques for optimizing app-initiated network traffic.
Batch and Schedule Network Requests
On a mobile device, the process of turning on the radio, making a connection, and keeping the radio awake uses a large amount of power. For this reason, processing individual requests at random times can consume significant power and reduce battery life. A more efficient approach is to queue a set of network requests and process them together. This allows the system to pay the power cost of turning on the radio just once, and still get all the data requested by an app.
Using a network access scheduler API for queuing and processing your app data requests can significantly increase the power efficiency of your app. Schedulers conserve battery power by grouping requests together for the system to process. They can further improve efficiency by delaying some requests until other requests wake up the mobile radio, or waiting until the device is charging. Schedulers defer and batch network requests system-wide, across all apps on the device, which gives them an optimizing advantage over what any individual app can do.
Choosing a batch-and-scheduling API
Android provides three different APIs for your app to batch and schedule network requests. For most operations, these techniques are functionally equivalent. These APIs are listed in the following table with the most highly recommended first.
Scheduler | Requirements | Implementation Ease |
---|---|---|
GCM Network Manager | GCM Network Manager requires that your app use the Google Play services client library, version 6.1.11 or higher — use the latest available version. | Straightforward |
Job Scheduler | Job Scheduler does not require Google Play services, but is available only when targeting Android 5.0 (API level 21) or higher. | Straightforward |
Sync Adapter for scheduled syncs | Sync Adapter does not require the Google Play services client library and has been available since Android 2.0 (API level 5). | Complex |
Note: For scheduled data synchronization, you should always prefer GCM Network Manager or Job Scheduler over Sync Adapter if your requirements allow it.
Allow System to Check for Connectivity
One of the most serious and unexpected causes of battery drain is when a user travels beyond the reach of any cell tower or access point. In this situation, the user is typically not using their device, but they notice the device getting warm, and then see that the battery is low or has run out.
In this scenario, the problem is that an app is running a background process that keeps waking up the mobile radio at regular intervals to search for a cellular signal, but finds none. Searching for a cell signal is one of the most power-draining operations there is.
The way to avoid causing this kind of problem for a user with your app is to use a battery-efficient method for checking connectivity. For app-initiated network requests, use a scheduler, which automatically usesConnectivity Manager to check for connectivity before calling into your app. As a result, if there's no network, the Connectivity Manager conserves battery because it performs the connectivity check itself, without loading the app to run the check. Battery is further conserved because schedulers use exponential backoff to check for connectivity less frequently as time progresses.