Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

google api client is connected return false

in my code the google api client returns the false value even it is being connected

GoogleApiClient mGoogleApiClient;
btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.i(TAG,"btn is clicked");
            buildGoogleApiClient();
            Log.i(TAG,"build google api completed");
            mGoogleApiClient.connect();
            if(mGoogleApiClient.isConnected())
            Log.i(TAG,"client conneted");
            Log.i(TAG,String.valueOf(mGoogleApiClient.isConnected()));


        }
});

protected synchronized void buildGoogleApiClient() {
    Log.i(TAG, "Building GoogleApiClient");
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
    createLocationRequest();
}

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

thanks in advance

like image 633
vitthal bhutekar Avatar asked Jul 05 '26 03:07

vitthal bhutekar


1 Answers

mGoogleApiClient.connect(); is not a synchronous operation - that's why you set up a addConnectionCallbacks(this) when building your GoogleApiClient.

You have to wait for the onConnected() callback before mGoogleApiClient.isConnected() will return true and you can use the GoogleApiClient.

like image 61
ianhanniballake Avatar answered Jul 07 '26 15:07

ianhanniballake



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!