GoogleApiClient is not connected yet, even though

2019-02-24 13:41发布

问题:

I looked at this Q&A here: GoogleApiClient is throwing "GoogleApiClient is not connected yet" AFTER onConnected function getting called

As it seemed to be similar to what I was experiencing but, it's not. The issue with that user was that they were declaring their api client in the onStart() method, I create mine in the onCreate() method like suggested by the answer. I am still however, getting the same error.

Here is the code I have for these three methods:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    /* Butter knife creates the variables */
    ButterKnife.bind(this);

    /* Startup location services */
    locationRequest = LocationRequest.create()
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
            .setInterval(10 * 1000)        // 10 seconds, in milliseconds
            .setFastestInterval(1 * 1000); // 1 second, in milliseconds

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

    progressBar.setVisibility(View.INVISIBLE);

    refreshImageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getForecast();
        }
    });
}

@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
    Log.i("Connected!!!", "WERE CONNECTED");
}

@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);
}

This is what the logger shows, which is confusing me since it shows were connected but the app crashes saying were not connected...

-14 02:25:13.582 25885-25885/? I/Connected!!!: WERE CONNECTED
11-14 02:25:13.582 25885-25885/? I/RESUMING: RESUMING LOCATION UPDATES
11-14 02:25:13.582 25885-25885/? D/AndroidRuntime: Shutting down VM
11-14 02:25:13.583 25885-25885/? E/AndroidRuntime: FATAL EXCEPTION: main
11-14 02:25:13.583 25885-25885/? E/AndroidRuntime: Process: lpadron.me.weatherly, PID: 25885
11-14 02:25:13.583 25885-25885/? E/AndroidRuntime: java.lang.RuntimeException: Unable to resume activity {lpadron.me.weatherly/lpadron.me.weatherly.MainActivity}: java.lang.IllegalStateException: GoogleApiClient is not connected yet.

回答1:

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();
}


回答2:

  1. Your class must implement GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener and override all the methods.

  2. GoogleApiClient will communicate with the LocationServices and give you the users latitude and longitude.

  3. In your OnCreate() method build a GoogleApiClient that uses the desired api.

  4. In your onStart() method start connecting the GoogleApiClient.

  5. On the Sucessfull connection OnConnected() method is called where we will be making our LocationRequest.

  6. Once we make the request onLocationChanged() method is called where we will be using the location object to get the latitude and longitude updates continuously.
  7. onConnectionSuspended(), onConnectionFailed() will be used when the connection is suspended and the connection is failed.
  8. Dont forget to disconect the GoogleApiClient connection in onStop() method else lot of resources will be wasted.

Watch here for tutorial Android Location API Using Google Play Services