nilkash
nilkash

Reputation: 7536

Not able to stop location updates with fused location APIs

Hi I am developing small android application in which I am trying to get location updates using fused location updates. I tried it in following way:

private void processStartLocation() {
    mGoogleApiClient.connect();
}

@Override
public void onConnected(Bundle bundle) {

    updateBooleanSharedPreference(this, "isLocationUpdatesOn", true);
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, locationPendingIntent);

    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    if (mLastLocation != null) {
        lat = String.valueOf(mLastLocation.getLatitude());
        lon = String.valueOf(mLastLocation.getLongitude());
        time = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss.SSS").format(new Date(mLastLocation.getTime()));
        updateUI();
    }
}

private void processStopLocation() {

    LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, locationPendingIntent);
}

So everything is working fine. I am able to receive my updates. But when I try to stop updates I got following issue:

java.lang.IllegalStateException: GoogleApiClient is not connected yet.
at com.google.android.gms.internal.zzlh.zzb(Unknown Source)
at com.google.android.gms.internal.zzli.zzb(Unknown Source)
at com.google.android.gms.location.internal.zzd.removeLocationUpdates(Unknown Source)

Am I doing anything wrong. Need some help. Thank you.

Upvotes: 1

Views: 3624

Answers (2)

badrobot15
badrobot15

Reputation: 186

Cannot see where or when you are calling processStopLocation() but that error generally means your 'mGoogleApiClient' object used in processStopLocation() has lost reference to the connected Google Api Client Object. Maybe a setter for the Google Api Client object in processStartLocation() and getter in processStopLocation() could work. Again without knowing exactly when the update start process and when the stop process is called it's hard to pin point the problem

Upvotes: 0

ishmaelMakitla
ishmaelMakitla

Reputation: 3812

Seeing that the error complaints about removing all location updates for the given pending intent (locationPendingIntent) because the GoogleApiClient is not connected - perhaps you should check for its connection status before making the call:

private void processStopLocation() {
    if(mGoogleApiClient !=null){
       LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
    }
    else{
     //no need to stop updates - we are no longer connected to location service anyway
    }
}

Upvotes: 3

Related Questions