title: Android LocationListener onLocationChanged() is never called
body: <p>I'm developing a Android app and I'm still green in this area.</p>
  
  <p>In my app I'm using the FusedLocationApi to get both the last know location and make Location update requests and I got a problem where, even with both Location and WiFi activated on Android Studio emulator and Samsung Galaxy S5, I never got the LocationListener's onLocationChanged() called.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>package com.trackit.app.locationupdatetest;
  
  import android.content.pm.PackageManager;
  import android.location.Location;
  import android.support.annotation.NonNull;
  import android.support.annotation.Nullable;
  import android.support.v4.app.ActivityCompat;
  import android.support.v4.app.FragmentActivity;
  import android.os.Bundle;
  import android.support.v4.content.ContextCompat;
  import android.util.Log;
  import android.view.View;
  import android.widget.Toast;
  
  import com.google.android.gms.common.ConnectionResult;
  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;
  import com.google.android.gms.maps.CameraUpdateFactory;
  import com.google.android.gms.maps.GoogleMap;
  import com.google.android.gms.maps.OnMapReadyCallback;
  import com.google.android.gms.maps.SupportMapFragment;
  import com.google.android.gms.maps.model.LatLng;
  import com.google.android.gms.maps.model.MarkerOptions;
  
  public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
  
  private static final String TAG = MapsActivity.class.getSimpleName();
  public static final String STARTING_POSITION = "Rio de Janeiro"; //Application's default position
  public static final int MY_PERMISSIONS_REQUEST_FINE_LOCATION = 1; //Fine location permission
  
  private GoogleMap mMap;
  private GoogleApiClient mGoogleApiClient;
  private LocationRequest mLocationRequest;
  private MapsActivity mActivity;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
  
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_maps);
  
      (findViewById(R.id.Location)).setEnabled(false);
      (findViewById(R.id.StreetView)).setEnabled(false);
  
      // Obtain the SupportMapFragment and get notified when the map is ready to be used.
      SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
              .findFragmentById(R.id.map);
      mapFragment.getMapAsync(this);
  
      if (mGoogleApiClient == null)
          buildGoogleAPIClient();
  
      //Create and configure a Location Request object to used while looking for location updates
      if(mLocationRequest == null)
          buildLocationRequest();
  
      mActivity = this;
  
  }
  
  private synchronized void buildGoogleAPIClient() {
  
      //Configuring the Google API client before connect
      mGoogleApiClient = new GoogleApiClient.Builder(this)
              .addConnectionCallbacks(this)
              .addOnConnectionFailedListener(this)
              .addApi(LocationServices.API)
              .build();
  
  }
  
  private synchronized void buildLocationRequest() {
  
      mLocationRequest = LocationRequest.create()
              .setInterval(10000)
              .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
              .setFastestInterval(1000)
              .setSmallestDisplacement(1);
  
  }
  
  @Override
  protected void onStart() {
      super.onStart();
      mGoogleApiClient.connect();
  }
  
  /**
   * This method is called when the Activity is no longer visible to the user.
   */
  @Override
  protected void onStop() {
      if(mGoogleApiClient.isConnected())
          mGoogleApiClient.disconnect();
      super.onStop();
  }
  
  /**    
   * This callback is triggered when the map is ready to be used.    
   */
  @Override
  public void onMapReady(GoogleMap googleMap) {
      mMap = googleMap;
  
      // Add a marker in Sydney and move the camera
      LatLng sydney = new LatLng(-34, 151);
      mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
      mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
  }
  
  public void buttonPressed(View buttonPressed){        
      displayUserLocation();      
  }
  
  @Override
  public void onConnected(@Nullable Bundle bundle) {      
  
      (findViewById(R.id.Location)).setEnabled(true);
      (findViewById(R.id.StreetView)).setEnabled(true);
  
  }
  
  @Override
  public void onConnectionSuspended(int i) {
  
  }
  
  @Override
  public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
  
      Log.e(TAG,"onConnectionFailed:"+connectionResult.getErrorCode()+","+connectionResult.getErrorMessage());
  
  }
  
  @Override
  public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull
  int[] grantResults) {      
  
      //Process the user permission's response
      switch (requestCode) {
          case MY_PERMISSIONS_REQUEST_FINE_LOCATION:
              processLocationPermissionResult(grantResults);
              break;
          default:
      }        
  
  }
  
  public void processLocationPermissionResult(@NonNull int grantResults[]) {
  
      // If request is cancelled, the result arrays are empty.
      if (grantResults.length &gt; 0
              &amp;&amp; grantResults[0] == PackageManager.PERMISSION_GRANTED)
          displayLocation();
      else {} // permission denied, boo! Disable the functionality that depends on this permission.        
  
  }
  
  public void displayUserLocation(){        
  
      //Verify if the app have permission to access user's location
      if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.
              ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  
          if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.
                  permission.ACCESS_FINE_LOCATION)) {
  
              //Asks the permission to access the user's location
              Toast.makeText(this, "This app needs to access your location. " +
                      "Allow the app to access it?", Toast.LENGTH_LONG).show();
  
          } else {
  
              ActivityCompat.requestPermissions(this,
                      new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
                      MapsActivity.MY_PERMISSIONS_REQUEST_FINE_LOCATION);
  
          }
  
      }   else displayLocation(); //If the permission was granted
  
  }
  
  public void displayLocation() throws SecurityException{
  
      Location lastLocation = LocationServices.FusedLocationApi.
              getLastLocation(mGoogleApiClient);  
  
      if(lastLocation != null) {
  
          Log.e(TAG, "!!!!!!");
  
          LatLng position = new LatLng(lastLocation.getLatitude(), lastLocation.
                  getLongitude());
          mMap.addMarker(new MarkerOptions().position(position).title("Marker in " +
                  MapsActivity.STARTING_POSITION));
          mMap.moveCamera(CameraUpdateFactory.newLatLng(position));
  
      }   else{
  
          Log.e(TAG, "??????");
          LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
                  mLocationRequest, this);
  
      }        
  
  }
  
  @Override
  public void onLocationChanged(Location location) {
  
      if(location.getAccuracy() &lt; 10 &amp;&amp; location.getSpeed() &lt; 55.55555555555556){
          Log.e(TAG, "onLocationChanged() started");          
  
          LatLng position = new LatLng(location.getLatitude(), location.getLongitude());
          mMap.addMarker(new MarkerOptions().position(position).title("Marker in " +
                  MapsActivity.STARTING_POSITION));
          mMap.moveCamera(CameraUpdateFactory.newLatLng(position));
  
          LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
  
          Log.e(TAG, "onLocationChanged() terminated");
      }
      /*else{
          //Continue listening for a more accurate location
      }*/
  
  }
  }
  </code></pre>
  
  <p>Below are both my AndroidManifest.xml and my gradle:</p>
  
  <p>AndroidManifest.xml</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="utf-8"?&gt;
      &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.trackit.app.locationupdatetest"&gt;
  
          &lt;!--
               The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
               Google Maps Android API v2, but you must specify either coarse or fine
               location permissions for the 'MyLocation' functionality. 
          --&gt;
          &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
          &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt;
          &lt;uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /&gt;
          &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
          &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
  
          &lt;uses-feature
              android:glEsVersion="0x00020000"
              android:required="true"/&gt;
  
          &lt;application
              android:allowBackup="true"
              android:icon="@mipmap/ic_launcher"
              android:label="@string/app_name"
              android:supportsRtl="true"
              android:theme="@style/AppTheme"&gt;
  
              &lt;!--
                   The API key for Google Maps-based APIs is defined as a string resource.
                   (See the file "res/values/google_maps_api.xml").
                   Note that the API key is linked to the encryption key used to sign the APK.
                   You need a different API key for each encryption key, including the release key that is used to
           sign the APK for publishing.
                   You can define the keys for the debug and release targets in src/debug/ and src/release/. 
              --&gt;
              &lt;meta-data
                  android:name="com.google.android.geo.API_KEY"
                  android:value="@string/google_maps_key" /&gt;
  
              &lt;activity
                  android:name=".MapsActivity"
                  android:label="@string/title_activity_maps"&gt;
                  &lt;intent-filter&gt;
                      &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                      &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
                  &lt;/intent-filter&gt;
              &lt;/activity&gt;
          &lt;/application&gt;
  
      &lt;/manifest&gt;
  </code></pre>
  
  <p>Gradle</p>
  
  <pre><code>apply plugin: 'com.android.application'
  
  android {
      compileSdkVersion 24
      buildToolsVersion "24.0.1"
  
      defaultConfig {
          applicationId "com.trackit.app.locationupdatetest"
          minSdkVersion 21
          targetSdkVersion 24
          versionCode 1
          versionName "1.0"
      }
      buildTypes {
          release {
              minifyEnabled false
              proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
          }
      }
  }
  
  dependencies {
      compile fileTree(dir: 'libs', include: ['*.jar'])
      testCompile 'junit:junit:4.12'
      compile 'com.android.support:appcompat-v7:24.2.0'
      compile 'com.google.android.gms:play-services:9.4.0'
  }
  </code></pre>
  
  <p>I'v searched a lot this week but I've got no helpful answer yet =s</p>
  
  <p>These are some of the threads I've searched (I don't want to look like a lazy guy, because I'm not, I'm just stuck)</p>
  
  <p><a href="https://stackoverflow.com/questions/38325383/onlocationchanged-does-not-get-called-using-fusedlocationapi-requestlocationupda">onLocationChanged does not get called using fusedLocationAPI.requestLocationUpdates</a> </p>
  
  <p><a href="https://stackoverflow.com/questions/28391906/unable-to-get-location-updates">Unable to get location updates</a></p>
  
  <p><a href="https://stackoverflow.com/questions/18109542/onlocationchanged-not-called-on-some-devices">onLocationChanged not called on some devices</a></p>
  
  <p><a href="https://stackoverflow.com/questions/28280435/on-locaton-changed-never-gets-called-in-android-google-client-api">on locaton changed never gets called in android google client api</a></p>
  
  <p><a href="https://stackoverflow.com/questions/34812845/onlocationchanged-isnt-being-called">onLocationChanged isn&#39;t being called</a></p>
  
  <p>Can someone point my errors? =S</p>