I’m looking to make a notifier type app for Android. Just sits in the background and performs a lookup via Internet every so often, and notifies of important incoming data.
Whats the best way to do something like this? How can I make it run only at selected intervals, so as to be battery friendly?
The Gmail notifications in Android seem to check the email as soon as I connect to the Internet. Is there a way for my code to do the same?
Thanks!
Right off the bat, I should mention that the technique you describe in your question is called polling. The thing with polling is that isn’t very battery/resource friendly and you usually want to stay away from it as much as possible (in the mobile realm).
Now given the description of your problem, what you want to look into is a push solution. That’s how the Android’s Gmail client works. Android’s native push solution is called C2DM and it’s freely available for versions 2.1+.
Now how would this work?
The general idea would be this: Using Gmail as the example, say the server (i.e. your backend) receives a new email for user
X. Your server will then send a targeted message to your app installed onX‘s device at which point your app will contact your server and retrieve the message.Now let me briefly describe polling:
In the latter example, your app consumed all those extra resources without actually accomplishing anything whereas in the push scenario, it was one call to the server to retrieve the message. You can easily see how bad polling can be on battery life.