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?