The optimal frequency of regular updates will vary based on device state, network connectivity, user behavior, and explicit user preferences.
Optimizing Battery Life discusses how to build battery-efficient apps that modify their refresh frequency based on the state of the host device. That includes disabling background service updates when you lose connectivity and reducing the rate of updates when the battery level is low.
This lesson will examine how your refresh frequency can be varied to best mitigate the effect of background updates on the underlying wireless radio state machine.
Use Firebase Cloud Messaging as an Alternative to Polling
Every time your app polls your server to check if an update is required, you activate the wireless radio, drawing power unnecessarily, for up to 20 seconds on a typical 3G connection.
Firebase Cloud Messaging (FCM) is a lightweight mechanism used to transmit data from a server to a particular app instance. Using FCM, your server can notify your app running on a particular device that there is new data available for it.
Compared to polling, where your app must regularly ping the server to query for new data, this event-driven model allows your app to create a new connection only when it knows there is data to download. The model minimizes unnecessary connections and reduces latency when updating information within your app.
FCM is implemented using a persistent TCP/IP connection. While it's possible to implement your own push service, it's best to use FCM. This minimizes the number of persistent connections and allows the platform to optimize bandwidth and minimize the associated impact on battery life.
Network scheduling tools
If your app requires polling, the Android platform provides several tools to help you create an efficient schedule for completing networking tasks such as polling. To learn more about using these tools, see the Guide to background processing.p>