I looked at this Q&A here: GoogleApiClient is throwing "GoogleApiClient is not connected yet" AFTER onConnected function getting called
As it seemed to
Your problem is in the onResume
logic:
@Override
protected void onResume() {
super.onResume();
if (!mGoogleApiClient.isConnected()) {
mGoogleApiClient.connect();
}
resumeLocationUpdates();
}
private void resumeLocationUpdates() {
Log.i("RESUMING", "RESUMING LOCATION UPDATES");
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this);
}
The call to mGoogleApiClient.connect()
is asynchronous. It returns before the connect is finished, and you are requesting location updates before the client is connected. You need to move the requestLocationUpdates
call to the GoogleApiClient.onConnected
callback. After this event, your client is connected.
@Override
protected void onResume() {
super.onResume();
if (!mGoogleApiClient.isConnected()) {
mGoogleApiClient.connect();
}
}
@Override
public void onConnected(Bundle bundle) {
resumeLocationUpdates();
}
Your class must implement GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener and override all the methods.
GoogleApiClient will communicate with the LocationServices and give you the users latitude and longitude.
In your OnCreate() method build a GoogleApiClient that uses the desired api.
In your onStart() method start connecting the GoogleApiClient.
On the Sucessfull connection OnConnected() method is called where we will be making our LocationRequest.
Watch here for tutorial Android Location API Using Google Play Services