views:

457

answers:

2

Hi,

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 stopping the service sufficient? Or do I need to stop the LocationManager in the onDestroy of the service?

Thank you for your help

+1  A: 

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.

Aidanc
Thanks for your reply. By "the values returned (lat,lng) by the Location Manager will be equal to null) you mean in the onLocationChanged handler? You don't recommend trying to get their location based on cell tower before using reverse geocoding?
Andrew
+3  A: 

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: http://stackoverflow.com/questions/3145089/what-is-the-simplest-and-most-robust-way-to-get-the-users-current-location-in-an/3145655#3145655

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.

Mathias Lin
Thank you for your response. I think I'll have my service provider also track NETWORK_PROVIDER (this will use cell towers, yes?) and if the GPS hasn't returned values (indoors it takes foreeeeever), I will use this data.
Andrew
Yes, network uses cell towers or IP address or both. see this link for detailed description: http://forum.sdx-developers.com/android-2-1-development/cdma-lockup-wifi-use-wireless-networks-and-gps!/msg22834/#msg22834 "Use Wireless Networks = this is only pinpointing that latitude and longitude of the stationary cell site, it is based off the IP address of the T1 at the cell site. It is using only one site, the site you are currently on, and that is what your seeing on the map, not YOUR location, but the cell site you are on." I suggest to use both GPS and Network provider at the same time.
Mathias Lin
Mathias Lin
Thank you for your responses. I'll put them to good use
Andrew