I'm starting to dip my toes into LocationManager. I've created a service which tracks GPS location (interval is more than 60000ms) and updates my application with user location so they can find stuff nearby.
As I understand it, users can go into their preferences and disable GPS for a particular application.
I would essentially like to offer my users two modes. 1) GPS. 2) If GPS can not pull, device does not have a GPS chip, or GPS is turned off for my app, I would like to allow them to type in their zip code (or maybe switch to cell tower lookup).
The bolded text is what I'm interested in. How can I tell if the device does not have GPS? How can I tell if the user has disabled GPS for my application?
Side question: My service gets launched from my 2nd Activity. I call stopService in the onDestroy method of this Activity. If I back up to my 1st Activity and the 2nd Activity is destroyed, I still notice the GPS icon on my phone running. Is s开发者_C百科topping the service sufficient? Or do I need to stop the LocationManager in the onDestroy of the service?
If a user doesn't have GPS activated, he could still have Wifi/Network activated as a location source in his phone settings, therefore you could use those data as well.
You can always get the last known location of the GPS or Network via
locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
and/or
locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)
It holds the last known location by the device, which can be from your app or from any other app that has used GPS before.
You can actually use two location managers with different providers: one with GPS_PROVIDER and another with NETWORK_PROVIDER. Gps is more accurate but usually only works outdoors under free sky why NETWORK_PROVIDER works indoors but is less accurate. Also, GPS might take a while until it gets a first fix after initialized (sometimes i.e. a minute or more), while the network get's the location almost immediately.
You should read this post, it's very detailed and has sample code: What is the simplest and most robust way to get the user's current location on Android?
Regarding the second question: before stopping the service, you should also unregister the location listener. You can do this onPause or onDestroy.
locationManager.removeUpdates(baseLocationListener)
or
locationManager.removeAllUpdates()
I think in your case using getLastKnowLocation() is fine.
Nevertheless, if you want to explicitly know about the GPS status, you can implement a GPS Status Listener and then use something like below, which tells you if the GPS has any satellite fix or not - but as said, I don't think this is really necessary.
final class GpsStatusListener implements GpsStatus.Listener {
public void onGpsStatusChanged(int i) {
if (i==GpsStatus.GPS_EVENT_FIRST_FIX) {
Log.d(AskTingTing.APP, "gpsx.fixed.");
for (GpsSatellite sat:locationManager.getGpsStatus(null).getSatellites()) {
Log.d(AskTingTing.APP, "gpsx.GpsStatus.Sat.fixed: " + sat.usedInFix());
}
}
}
}
Look at the link above for a good code sample.
If the device does not have GPS or it's not available the values returned (lat,lng) by the Location Manager will be equal to null
so just check the values and then prompt the user to enter their location. I'd recommend Reverse Geocoding to get a lat/lng value for their location in that instance.
side question: I'm not as sure on I assume you'd need to Destroy it if it's still running.
精彩评论