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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With