GoogleApiClient is not connected yet, even though onConnected is called and I'm creating my GoogleApiClient in onCreate

前端 未结 2 1159
长发绾君心
长发绾君心 2021-01-20 18:38

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

As it seemed to

2条回答
  •  情话喂你
    2021-01-20 19:06

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

提交回复
热议问题