0

My onLocationChanged is not called at all. Any ideas why? I have all the permissions. Here is my code:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_locate_me);

    createLocationRequest();
  buildGoogleApiClient();

    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

    MapFragment mapFragment = ((MapFragment) getFragmentManager().findFragmentById(R.id.map));
    mapFragment.setRetainInstance(true);
}

@Override
protected void onStart() {
    super.onStart();
    if (mGoogleApiClient != null) {
        mGoogleApiClient.connect();
    }
}

protected synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
}

protected void createLocationRequest() {
    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(10000);
    mLocationRequest.setFastestInterval(5000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}

@Override
public void onConnected(Bundle bundle) {
    if (mRequestingLocationUpdates) {
        startLocationUpdates();
    }
}

@Override
public void onConnectionSuspended(int i) {
}

@Override
public void onLocationChanged(Location location) {
    mCurrentLocation = location;

    displayMessage("message","my position changed");

}


protected void startLocationUpdates() {
    LocationServices.FusedLocationApi.requestLocationUpdates(
            mGoogleApiClient, mLocationRequest, this);
}


@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}

private void displayMessage(String title, String message) {
   //displaying a message
}


@Override
protected void onPause() {
    super.onPause();
    stopLocationUpdates();
}

protected void stopLocationUpdates() {
    LocationServices.FusedLocationApi.removeLocationUpdates(
            mGoogleApiClient, this);
}

@Override
public void onResume() {
    super.onResume();
    if (mGoogleApiClient.isConnected() && !mRequestingLocationUpdates) {
        startLocationUpdates();
    }
}

I followed android tutorial and it all seems to be the same, but onLocationChanged isn't being called (in this case message is not displayed). Btw, it seemed to have worked about 3 weeks ago, I don't know what changed.

suue
  • 295
  • 6
  • 17
  • How long are you waiting for a location update? You may also want to set `mLocationRequest.setSmallestDisplacement(desiredResolutionInMeters)`. – mattm Jan 15 '16 at 14:24
  • Long enough i think, even after a couple of minutes there is still no result. When it worked it was done instantly. – suue Jan 15 '16 at 14:32
  • What feedback exactly are you expecting `onLocationChanged`? You have not implemented `displayMessage`. – mattm Jan 15 '16 at 20:00

1 Answers1

1

I suspect the problem is somewhere with mRequestingLocationUpdates. There is no indication in your code provided when, if at all, mRequestingLocationUpdates is set.

Also for location updates with physical devices, the time to get a location fix depends on what is allowed on the device: Settings->Location. First, location needs to enabled. The mode will determine whether GPS and WiFi or Bluetooth location are enabled. WiFi and Bluetooth location should be very fast (few seconds) as long as you are in an area with WiFi/Bluetooth coverage and you have a data connection. GPS is very accurate, but can be very slow (minutes), depending on visibility to satellites in the sky and if you have a data connection for assisted GPS.

mattm
  • 5,851
  • 11
  • 47
  • 77
  • mRequestingLocationUpdates is a global variable set to true and location services are enabled, so neither one is a problem unfortunately – suue Jan 15 '16 at 15:08
  • and you verified that `startLocationUpdates()` is called and all parameters used there are correct? – WarrenFaith Jan 15 '16 at 15:20
  • @suue If `mRequestingLocationUpdates` is set to true, `onResume` will not call `startLocationUpdates`. – mattm Jan 15 '16 at 16:36
  • but onConnected should, right? @WarrenFaith startLocationUpdates() is called and parameters are correct – suue Jan 15 '16 at 18:58