Luke
Luke

Reputation: 527

UPDATE : google play services must be connected - android

I have this error when i try to connect at the google play services.

I follow this guide , no more than four hours ago, https://developer.android.com/google/play-services/setup.html

can someone help me?

UPDATE:

after a google play services update in my device i no longer have this error , but it still does not work.

This is my code:

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.games.Games;
import com.google.android.gms.plus.Plus;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class MainActivity extends Activity 
    implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,
    View.OnClickListener {

GoogleApiClient mGoogleApiClient;

int REQUEST_ACHIEVEMENTS = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button one = (Button) findViewById(R.id.one);
    Button two = (Button) findViewById(R.id.two);
    Button three = (Button) findViewById(R.id.three);

    mGoogleApiClient = new GoogleApiClient.Builder(this)
                        .addApi(Plus.API)
                        .addScope(Plus.SCOPE_PLUS_LOGIN)
                        .setAccountName("account name (i hid) ")
                        .build();

    one.setOnClickListener(this);
    two.setOnClickListener(this);
    three.setOnClickListener(this);



}
@Override
protected void onStart() {
    Log.d("MYTAG", "onStart()");
    super.onStart();

    mGoogleApiClient.connect();
    int result = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
    Log.d("MYTAG", "Result: " + result);
    GooglePlayServicesUtil.getErrorString(result);
  }

  /*  protected void onResume(){
    int result = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
    if( result == ConnectionResult.SUCCESS){
        Log.d("MYTAG", "SUCCESS");
        GooglePlayServicesUtil.getErrorString(result);
    }else{
        if( result == ConnectionResult.SERVICE_MISSING){
            Log.d("MYTAG", "service missing");
        }
        if( result == ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED){
            Log.d("MYTAG", "Service version update required");
        }
        if( result == ConnectionResult.SERVICE_DISABLED){
            Log.d("MYTAG", "Service disabled");
        }
        if( result == ConnectionResult.SERVICE_INVALID){
            Log.d("MYTAG", "Service invalid");
        }

    }
}*/
protected void onStop() {
    Log.d("MYTAG", "onStop()");
    super.onStop();
    if (mGoogleApiClient.isConnected()) {
      mGoogleApiClient.disconnect();
    }
  }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}


@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()){

    case R.id.one:

        Log.d("MYTAG", "ACHIEVEMENT BUTTON CLICKED");
        //BaseGameUtils.showAlert(this, getString(R.string.you_won));
        if (mGoogleApiClient.isConnected()) {
          // unlock the first achievement.
           Games.Achievements.unlock(mGoogleApiClient, "here there is my achivements id ( i hid) ");
        }else{
            Log.d("MYTAG", "NOT CONNECTED");

        }
        break;
    case R.id.two:

        startActivityForResult(Games.Achievements.getAchievementsIntent(mGoogleApiClient), REQUEST_ACHIEVEMENTS);

        break;
    case R.id.three:
        mGoogleApiClient.connect();

        break;
    }

}


@Override
public void onConnectionFailed(ConnectionResult arg0) {
    // TODO Auto-generated method stub

    Log.d("MYTAG", arg0 + "");

}


@Override
public void onConnected(Bundle arg0) {
    // TODO Auto-generated method stub

}


@Override
public void onConnectionSuspended(int arg0) {
    // TODO Auto-generated method stub

}

}

no more error in log cat, but if i press the button 2 appears this error :

 01-04 23:49:41.725: E/AndroidRuntime(2211): java.lang.IllegalStateException: GoogleApiClient must  be    connected.

if i press button 3 , no message, no error.

But the result of GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); is equal to 0 , (success) .

Upvotes: 1

Views: 670

Answers (1)

ianhanniballake
ianhanniballake

Reputation: 199825

Per the same page you linked:

To verify the Google Play services version, call isGooglePlayServicesAvailable(). If the result code is SUCCESS, then the Google Play services APK is up-to-date and you can continue to make a connection. If, however, the result code is SERVICE_MISSING, SERVICE_VERSION_UPDATE_REQUIRED, or SERVICE_DISABLED, then the user needs to install an update. So, call GooglePlayServicesUtil.getErrorDialog() and pass it the result error code. This returns a Dialog you should show, which provides an appropriate message about the error and provides an action that takes the user to Google Play Store to install the update.

Similarly, if you are connecting via a GoogleApiClient, then calling startResolutionForResult() using the result returned to you in onConnectionFailed() will create this same dialog and ask the user to upgrade their Google Play services.

Upvotes: 1

Related Questions