问题
I have some ideas for an app that checks your location about every 5 minutes or less. Maybe every 1 minute, I'm not sure yet. Obviously I can simply create a service that checks what the phone's GPS location is every N minutes. But that is a potential drain on the battery.
What I was wondering, are there any internal services that are already running in the Android OS that are already polling for the phones GPS location? That way perhaps I can simply ask those services for what the location is instead of making the GPS location request myself, in order to save on battery usage.
回答1:
No such system services exist. You can create a service and periodically check for lastKnownLocation().
Designing a location-aware app depends on a lot of factors. Try answering this questions:
- Do you always need GPS location or is network based location good enough?
- How "fresh" does a location need to be?
- Do you need a location even if user does not move?
- Do you need a location even if phone is in sleep mode?
Read this http://android-developers.blogspot.com/2011/06/deep-dive-into-location.html
回答2:
When you use the Location Service of Android you subscribe for location updates. You can do that from a service. When you subscribe, you indicate how often you would like to receive notification (minimum update time), but also how much your postion need to change in order for an update to be received (minimum distance).
So if you do not move, you only receive your position once. So you are not running constantly. It is your job to live with the fact that you only receive your position once.
Look at the android location API for further information:
Location API entrypoint and look for the requestLocationUpdates method
来源:https://stackoverflow.com/questions/8124142/android-are-there-query-able-services-that-already-poll-for-gps-location