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 开发者_Go百科when call requestLocationUpdates()?
thanks,
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..
GPS aren't working when screen is off! You may set alarm for you app, but to get GPS position you must make screen on.
Updated at 2016-08-23:
Please be note, this answer is old and this information not actual for last os versions.
精彩评论