ntaj
ntaj

Reputation: 311

Android: How to send location information to server while app is inactive

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:

  1. Main Android app is inactive (i.e., hasn't been launched)
  2. Based on GPS location of the device, the server may push a message (via C2DM) to the device
  3. 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?)

Upvotes: 0

Views: 1569

Answers (2)

bindal
bindal

Reputation: 1932

You, can simply create a background service then implements a location listener. In that location listener, you can set the time interval in the requestLocationUpdate method to ping the GPS at particular time intervals. Here is a location manager instance:

lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, timeinterval2, 0, locationListener);

You can send that location to the server from the service. You can also send notifications to launch the app.

Upvotes: 0

Jordi Coscolla
Jordi Coscolla

Reputation: 1066

I would not use just a Service, Better set an Alarm using AlarmManager each (5 minutes, 15 minutes or what you need) and then get the location. @CommonsWare have done some code thats quite useful ( it deals also with WEAK_LOCKS so 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.

Upvotes: 0

Related Questions