Sainik
Sainik

Reputation: 165

LocationServices.FusedLocationApi.requestLocationUpdates onLocationChanged not being called back

I am trying to get GPS location every 1 min or 100 meter distance. But onChangeLocation is never called back as per given constrains on device.

What mistake I have done in code

permission in manifest

** uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"**

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;


    @Overrid
    public void onCreate() {

            this.googleApiClient = new GoogleApiClient.Builder(this)
                    .addApi(LocationServices.API)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .build();

            LOCATION_REQUEST =
                    LocationRequest.create().setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY).setFastestInterval(1000).
                            setExpirationTime(1000);

            this.googleApiClient.connect();
    }

    @Override
    public void onConnected(Bundle bundle) {

        isGoogleClientConnected = true;
        if (this.googleApiClient.hasConnectedApi(LocationServices.API)) {
            Toast.makeText(getApplicationContext(), "LocationServices is available.", Toast.LENGTH_LONG).show();
                LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, LOCATION_REQUEST, this);
                       }
    }

    /**
     * @param i
     */
    @Override
    public void onConnectionSuspended(int i) {
        Toast.makeText(getApplicationContext(), "location connection disconnected", Toast.LENGTH_LONG).show();
        LocationServices.FusedLocationApi.removeLocationUpdates(this.googleApiClient, this);
        isGoogleClientConnected = false;
    }

    /**
     * @param connectionResult
     */
    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        android.util.Log.e(Global.TAG, String.valueOf(connectionResult.getErrorCode()));
        isGoogleClientConnected = false;
        // try to reconnect is fails in infinite loop
        this.googleApiClient.reconnect();
    }

public void onLocationChanged(Location location) {
        Toast.makeText(getApplicationContext(), "onLocationChanged is called", Toast.LENGTH_LONG).show();

    }

Upvotes: 3

Views: 877

Answers (1)

Kazuki
Kazuki

Reputation: 1492

Try removing setExpirationTime

Currently your request is

.setFastestInterval(1000).setExpirationTime(1000)

you ask not to callback until 1000ms pass and not to callback after 1000ms

Upvotes: 0

Related Questions