Search code examples
androidpower-management

Android battery when working with http


Recently google introduced push-to-device service, but it's only available 2.2 and up.

I need a similar system in my app, and I'm trying to get around limitations.

The issue is battery life. Since the user must be notified immediately about the changes on the server, I thought to implement a service that would live in the background (standard Android service) and query the server for updates.

Of course, querying the server, even each second, will cost a lot of bandwidth, as well as battery, so my question is this: does it make a difference if the server is holding the response for some period of time? (the idea behind Comet type ajax request)

Works like this:

  • Device sends request for data update
  • Server gets the request and goes in the loop for one minute, checking if there are updates on each iteration
    • If there are updates, server sends response back with updates
    • If not, service goes on to the next iteration.
  • After a minute, it finally sends the response that no data is yet available
  • After response (no matter whether empty or with data) Android fires another such request.

It will definitely cost less bandwidth, but will it consume less (or even more) battery?


Solution

  • Holding a TCP socket (and consequently waiting for an HTTP response) as you suggest is probably going to be your best option. What you've described is actually already implemented via HTTP continuation requests. Have a look at the Bayeux protocol for HTTP push notifications. Also, check out the Android implementation here. For what it's worth, that's definitely what I would use. I haven't done any sort of analysis of it, but this allows you to minimize the amount of data transmitted over the line (which is directly proportional to the power consumption) by allowing the connection to hang for as long as possible.

    In short, the way Bayeux works is very similar to what you've suggested. The client opens a request and the server waits on it. If it has something to send, it sends it otherwise it simply waits. Eventually, the request will timeout. At that point, the client makes another request. What you attain is near instantaneous push to the client from the server without constant polling and duplication of information like HTTP headers, etc.