5

I'm working on an app which consists of a service and an activity. The service uses a LocationClient-object to request the current location every minute. The activity uses another LocationClient-object to request a single current location after a button was clicked.

In the service:

mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(60000);
mLocationRequest.setFastestInterval(60000);
mLocationClient = new LocationClient(this, this, this);
// ... when connected:
mLocationClient.requestLocationUpdates(mLocationRequest, this);

In the activity:

mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(0); // 0 should be ok since only a single location is requested
mLocationRequest.setFastestInterval(0);
mLocationRequest.setNumUpdates(1); // get only a single location
mLocationClient = new LocationClient(this, this, this);
// ... when connected:
mLocationClient.requestLocationUpdates(mLocationRequest, this);

The service alone works like expected.

However if the service is started and the activity tries to get a single current location then it receives the location not until the service receives an updated location. So the activity has to wait up to 60 seconds.

But if the service is not started and the activity tries to get a single current location then it receives the location after a short time like expected (usually < 5 sec.).

What is causing the problem? Is only one LocationListener per app allowed?

Biggie
  • 7,037
  • 10
  • 33
  • 42

1 Answers1

1

First - if you need an immediate location don't request location updates just call getLastLocation() on your location client. That will give you the best/most recent location result if the LocationClient has one available. If it does not, then I would request updates.

Second - I don't think there's a limit on how many location clients you can have in a single app, however, I think you're missing how those clients interact with Google Play Services to receive updates. Looking at the docs provides some insight.

If you scroll down to the part about specifying update parameters you will see that LocationClient sort of sits between you and the location data you seek. All other apps on the phone implementing LocationClient seek to access the same data. LocationClient tries fill your request within the interval you asked for. However, because other apps using LocationClient may be trying to access the same data, the timing is not guaranteed. The best example of this is setFastestInterval():

Calling LocationRequest.setFastestInterval() also helps to save power. When you request a preferred update rate by calling LocationRequest.setInterval(), and a maximum rate by calling LocationRequest.setFastestInterval(), then your app gets the same update rate as the fastest rate in the system. If other apps have requested a faster rate, you get the benefit of a faster rate. If no other apps have a faster rate request outstanding, your app receives updates at the rate you specified with LocationRequest.setInterval().

When you receive an update is not directly linked to when you requested it. Sometimes it will be sooner, sometimes it will be later. The intervals are merely a preference.

Rarw
  • 7,645
  • 3
  • 28
  • 46
  • I know that the timing is not guaranteed. ;-) But if I understand the documentation correctly then the Location service tries to use the smallest possible interval given by the registered LocationRequests. In my case this is the one of the activity, not the one of the service. Another example: If "My tracks" is tracking the location every minute in the background, then my activity can still get the current location much faster (~3 sec) - without knowing the internals of "My tracks". However using the two LocationClients above my Activity is always waiting the time set by the service. – Biggie Jan 23 '14 at 19:34
  • Note the documentation says: "This method sets the rate in milliseconds at which your app prefers to receive location updates. If no other apps are receiving updates from Location Services, your app will receive updates at this rate.". The documentation talks about "the app" and not about the "LocationClient/LocationListener-object". That's why I asked about problems having multiple LocationClients in the same app. For me it seems like the first used interval in any of the app's LocationClients wins. In my case the 60-second interval of the service. – Biggie Jan 23 '14 at 19:54
  • Well there's nothing in the documentation that limits LocationClient to one instance per app. But the point that updates are app-wide is worth exploring. That's kind of my point with Google Play Services. There's only one instance/version of that per phone. If you have to request updates through the LocationClient from play services, I would imagine the last request would be the one followed. You should switch the values and see if you get the opposite result. – Rarw Jan 23 '14 at 20:05
  • After setting the interval to 0 for the service and 60000 for the activity the service is getting the locations very fast and after clicking the button in the activity it gets the location almost immediately. – Biggie Jan 23 '14 at 20:16
  • Today I made the test mentioned above: Using the "My Tracks"-app in the background to track locations very fast and my service at an 60-sec interval allows the activity to get the current location very fast. Without running "My tracks" in the background the activity must wait the 60-sec interval of the service. – Biggie Jan 24 '14 at 12:16
  • So its dependant on the last request made? I'm assuming the service is requesting updates after the activity launches because you have to start the service from somewhere. I tried to find the source for LocationClient but I think its in Google Play which I'm pretty sure is not open. – Rarw Jan 24 '14 at 14:25
  • 1
    Yes, the activity starts the service when a button is clicked. But the activity requests location updates not until the user clicks (another) button. So the activity requests updates after the service has requested updates. So it looks like the first request wins. – Biggie Feb 03 '14 at 17:42