3

In my app I have an AlarmManager set to run an IntentService every so often to make a WebService Call to my server sending your current location

When the alarm is started I also start a Service that has a LocationManager to keep track of the location locally.

When I make the web service call to my server I use

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);

        String provider = locationManager.getBestProvider(criteria, true);
        Location lastKnownLocation = locationManager.getLastKnownLocation(provider);

since I only need the location at this point in time and the next time the web service is called again I just get it again. But it appears even though I have a service running in my background that uses the GPS and gets the right location, calling

locationManager.getLastKnownLocation(provider);

when I make the web service to get the location it gives me a stale location and not the current one. Now I would think that since I already have a locationmanager running in another service that the getLastKnownLocation would get updated to the correct location everytime the onLocationChanged gets called but that is obviously not the case which makes no sense to me.

So my question now is how can I get my correct location in my IntentService since I cannot rely on getLastKnownLocation

I do no need to know the location everytime it changes I just need to know what it is at the time I make the web service call so starting another location manager does not seem like a good idea. The only thing I can think of to solve this is to store the location from my other service in SharedPreferences and just grab whatever is in there but that seems a little hacky and i dont like hacky.

any ideas?

tyczj
  • 71,600
  • 54
  • 194
  • 296
  • 1
    Just a suggestion: You should be using FusedLocationProvider if you can. https://developer.android.com/google/play-services/location.html – hwrdprkns Jun 03 '13 at 18:54
  • @hwrdprkns I tried that but the IntentService ends before the `onConnect` gets called throwing an `IllegalStateExcepion` saying I need to call `onConnect` before attempting to call `getLastLocation()` so basically the service is getting destroyed before I can get to it – tyczj Jun 03 '13 at 19:42

1 Answers1

3

The last known location for android gets updated only when any app requests for a location (app can be yours or any other on the device). You have to implement the location listener and then use locationManger.requestLocationUpdates(provider, minTime, minDistance, locationListener) to ask for the location update. The android system will get the location update on your request and set the lastKnownLocation for all apps to use.

The only reason I can think of the lastKnownLocation not being set is that the android system couldn't get the location. Check that the provider requested for the location update is enabled. Also you can use Passive Provider for piggybacking on the location requests of other apps and actively look for location only if there is no update for certain time out period (This approach should be better that constantly trying to get location updates which are costly).

Srikant Sahay
  • 885
  • 6
  • 9
  • I know for a fact that I am getting location updates in my other service where I am getting a location via a `LocationListener` because I can go into the database where I store the locations and see all the different locations – tyczj Jun 03 '13 at 19:48
  • hmm... If the location updates are received, then the lastKnownLocation should have been updated. Strange.... Haven't encountered such a problem myself. – Srikant Sahay Jun 03 '13 at 20:19
  • Did a little more playing around today and it was indeed the locationmanager was not getting a fix on my location. It was sporadic but when I did get a fix the `getLastKnownLocation` did return the correct location – tyczj Jun 04 '13 at 19:10
  • Your are setting the criteria as fine. So the I believe that the locationmanager.getbestprovider will try to get the location fixed by gps. Gps requires some time to get a fix. It might be that you are not giving it enough time to get a fix (in your other service). – Srikant Sahay Jun 05 '13 at 02:59
  • I start the service and just let it run, the IntentService is on a polling interval (every 30 seconds). I was driving around and saw that it constantly lost GPS lock because in the notification bar it said searching for GPS – tyczj Jun 05 '13 at 13:59
  • Just poor reception I guess. Why not try gps first and then network provider and check which has more recent update. The network provider may not be as accurate but it's better than stale location IMO. – Srikant Sahay Jun 05 '13 at 14:03
  • One more quick question, I use strictly GPS in my service that listens for location updates, if I add in a network listener too would that also update the `getLastKnownLocation` too just incase the GPS cannot lock on like I saw? – tyczj Jun 05 '13 at 15:46
  • getLastKnownLocation(provider) gives the lastKnownLocation for the given provider. Check it for both gps provider as well as network provider and compare the time of update. Also when you request for updates and are registering and unregistering the location listener (to save battery as constant location updates eat up battery), try to give at least a couple of minutes to the location listener before giving up and unregistering it. A link that might be useful: http://stackoverflow.com/questions/6181704/good-way-of-getting-the-users-location-in-android – Srikant Sahay Jun 05 '13 at 20:00
  • so `getLastKnownLocation()` doesn't hook into Phone's constantly-tracked GPS? It only is set when an app uses a location listener?? – Don Cheadle Aug 23 '15 at 00:41