LocationServices.FusedLocationApi.requestLocationUpdates onLocationChanged is not returned

I am trying to get a GPS location every 1 min or 100 meters. But onChangeLocation is never called back according to the given restrictions on the device.

What mistake did I make in the 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();

    }

      

+3


source to share


1 answer


Try to remove setExpirationTime

Your request is currently



.setFastestInterval(1000).setExpirationTime(1000)

you are asking for no callback before 1000ms pass, not for callback after 1000ms

0


source







All Articles