What is the appropriate way to implement a location-based Android app that periodically sends its location to a server?
Here is the specific flow I would like to implement:
- Main Android app is inactive (i.e., hasn’t been launched)
- Based on GPS location of the device, the server may push a message (via C2DM) to the device
- Upon receiving the message, a notification should appear in the notification bar (which can then be used to launch the app)
My main confusion stems from point #1: how do I send GPS coordinates to the server if the app has not been launched? Is it simply a matter of implementing a service that periodically sends the phone location to the server? How can I ensure the service is always running once the user has installed the app?
(The Groupon app for Android does something similar, where you intermittently receive notifications of nearby deals even when the app is inactive. Any idea how they may have implemented this?)
I would not use just a
Service, Better set an Alarm usingAlarmManagereach (5 minutes, 15 minutes or what you need) and then get the location. @CommonsWare have done some code thats quite useful ( it deals also withWEAK_LOCKSso if the phone goes sleep everything still run etc..)Also, You don’t need to use C2DM because you must send your location to the server and the server response you with the geo-information. C2DM is only for the case that you not start the communication.