Im trying to retrieve the user's location once, then disconnect() from location services to avoid battery use. The problem is, even after i disconnect i see the location arrow icon in the status area - also the callback onDisconnected() never gets called, even after calling disconnect on the client.
This is what im doing:
In onResume()
i connect:
mLocationClient = new LocationClient(this, this, this);
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(LOCATION_UPDATE_INTERVAL);
mLocationRequest.setFastestInterval(1);
mLocationClient.connect();
In onConnected()
i try getting lastlocation, if its NOT null, ill get it and disconnect from location services. If it is null, i start listening for updates:
if (mLocationClient.getLastLocation() != null) {
userLocation = mLocationClient.getLastLocation();
stopLocationServices();
// do something which requires location
} else {
mLocationClient.requestLocationUpdates(mLocationRequest, this);
}
In onLocationChanged
userLocation = mLocationClient.getLastLocation();
stopLocationServices();
// do something which requires location
My stopLocationServices()
function looks like this:
private void stopLocationServices() {
if (mLocationClient != null) {
try {
mLocationClient.removeLocationUpdates(this);
mLocationClient.disconnect();
} catch (Exception e) {}
}
mLocationClient = null;
}
thanks for any help.