views:

169

answers:

1

I need to update the location through GPS in the background even when the phone is in sleep. I am thinking to use AlarmManager to broadcast an Intent and then a receiver will call requestLocationUpdates() on LocationManager. But I am not sure whether it's guaranteed that the GPS location will be updated when the phone is in sleep and an intent will be broadcasted if I registered a PendingIntent when call requestLocationUpdates()?

thanks,

+1  A: 

You could use a service.

A service doesn't have a visual user interface, but rather runs in the background for an indefinite period of time. For example, a service might play background music as the user attends to other matters, or it might fetch data over the network or calculate something and provide the result to activities that need it. Each service extends the Service base class.

If your phone go to sleep the service will continue its work in background. Remember to start the service using startService() instead of bindService() otherwise when the activity that start the service go to sleep the service will be stopped.

hope this helps..

hara
I didn't want to use service because service process might get killed by the system.
wei
You could make it a foreground service.
Andrew Guenther