-1

I have tried to follow to Google documentation for Google Play Services but that's impossible, because Google documentation is the worst ever.

Anyway, I manged to setup project and add Google Play Services in my app, but I don't know how to keep connected state of player when user changes activity.

I have MainActivity, where extend BaseGameActivity, and then user click on button Play, I open GameActivity, where I play game, and when I finish game I want to submit score to Google Play Services Leaderboard, but I can't because it seems that I am not connected.

I extend MyActivity with BaseGameActivity

 public class MainActivity extends BaseGameActivity

Then I on GameActivity extend this:

public GameActivity extends Activity implements GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener

And then on onCreate use this:

  // Create the Google Api Client with access to Plus and Games
   mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())
            .addOnConnectionFailedListener(this)
            .addApi(Plus.API).addScope(Plus.SCOPE_PLUS_LOGIN)
            .addApi(Games.API).addScope(Games.SCOPE_GAMES)
                    // add other APIs and scopes here as needed
            .build();

And then when I finish game I try to submit score, but it seems that I am not connected.

  if(mGoogleApiClient.isConnected()){
                Games.Achievements.unlock(mGoogleApiClient,
                        getString(R.string.app_name));
                Games.Leaderboards.submitScore(mGoogleApiClient,
                        getString(R.string.number_guesses_leaderboard),
                        clickCounter);
            }
Zookey
  • 2,637
  • 13
  • 46
  • 80

2 Answers2

1

According to the docs, you need to call mGoogleApiClient.connect() aswell. They recommend using a button for sign in, but you can also do it at onStart():

@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

If all goes well you'll receive the callback onConnected(Bundle connectionHint) which you are already implementing.

MadEqua
  • 1,142
  • 9
  • 19
1

Play Game Services are really made with Fragments in mind. It is almost mandatory to use Fragments instead of Activities, and to have a single Activity in your app...

Stéphane
  • 1,518
  • 1
  • 18
  • 31
  • Do you have any example beside of Google documentation? – Zookey Nov 06 '14 at 22:32
  • 1
    Nope, I dont. But I don't really understand why say the documentation is "the worst ever". It is kinda nicely done. Did you use the samples as a base, as it's recommanded on the documentation ? https://developer.android.com/google/play-services/games.html – Stéphane Nov 07 '14 at 08:21
  • I have opens new question based on sample from documentation, but I still get error with sign in. Take a look here: http://stackoverflow.com/questions/26804929/failed-to-sign-in-please-check-your-network-connection-and-try-again – Zookey Nov 07 '14 at 16:04