Quick handling of user requests helps ensure a good user experience, especially when it comes to user actions that require network access. You should prioritize low latency over power conservation to provide the fastest response when optimizing network use that is a direct result of user actions. Attaining an optimal network traffic profile for your app, while making sure that your users get fast responses, can be a bit challenging.
This lesson teaches you how to optimize network use for user-initiated actions and reduce battery consumption.
Pre-fetch Network Data
Pre-fetching data is an effective way to reduce the number of independent data transfer sessions that your app runs. With pre-fetching, when the user performs an action in your app, the app anticipates which data will most likely be needed for the next series of user actions and fetches that data in bulk. Battery power consumption is reduced for two reasons:
- Because your app pre-fetches data only when the mobile radio is already awake from the user's action, and so does not incur the overhead of waking up the mobile radio.
- The app pre-fetches data for anticipated user actions, each of which might otherwise require separate requests that each incur waking up the mobile radio.
Tip: To explore whether your app might benefit from pre-fetching, review your app's network traffic and look for situations where a specific series of user actions almost always results in multiple network requests over the course of the task. For instance, an app that incrementally downloads article content as a user views it might be able to pre-fetch one or more articles in categories the user is known to view.
Watch the video on effective pre-fetching which describes what pre-fetching is, where to use it, and how much data to pre-fetch. For more details, see Optimizing Downloads for Efficient Network Access.
Check for Connectivity or Listen for Changes
Searching for a cell signal is one of the most power-draining operations on a mobile device. Your app should always check for connectivity before sending a user-initiated network request. If you use a scheduling service, Schedulers do this automatically for you.
- If only certain buttons in your activity depend on a network connection, use Connectivity Manager to check for a network connection before sending the network request, as instructed in Monitor for Changes in Connectivity. If there's no network, the app can save battery by not forcing the mobile radio to search.
- If your entire activity's user interface is non-functional without network access, then use Manipulate Broadcast Receivers on Demand. This technique listens for connectivity changes when your activity is in the foreground, and prevents network requests from proceeding when no connectivity exists. That is, if your app detects that connectivity has been lost, it disables all of its receivers, except for the connectivity-change receiver. An example would be a news app that presents an activity with a full-screen view of news snippets and does no pre-fetching. Any snippet a user taps would require a network connection.
A best practice for user-initiated traffic is to first check for a connection using Connectivity Manager, and if there is no connection, schedule the network request for when the connection is made. Schedulers will use techniques such as exponential backoff to save battery, where each time the attempt to connect fails, the scheduler doubles the delay before the next retry.
Note: To check for connectivity for app-initiated traffic, see Optimizing App-Initiated Network Use.
Reduce the Number of Connections
In general, it's more efficient to reuse existing network connections than to initiate new ones. Reusing connections also allows the network to more intelligently react to congestion and related network data issues. For more information on reducing the number of connections used by your app, see Optimizing Downloads for Efficient Network Access.