how to connect to google play service and load leaderboard

六眼飞鱼酱① 提交于 2019-12-07 14:11:43

问题


I want to connect my game with google play service. i have read documentation on android developer and try to following type-a-number sample and still can't load leaderboard.

i have import baseGameUtils, but i use andengine so i didn't use extends BaseGameActivity from google.

what i have until now:
- GooglePlayServicesUtil.isGooglePlayServicesAvailable(this) return success
- startActivityForResult(pickAccountIntent, REQUEST_CODE_PICK_ACCOUNT); is working well and i got my account name from onActivityResult(..);
- i already put this on my manifest.

<meta-data android:name="com.google.android.gms.games.APP_ID"
        android:value="@string/app_id" />

my questions are
1. can i use google play service without extends BaseGameActivity?
2. if i use gameHelper.beginUserInitiatedSignIn(); after i got my account name, i got this on log cat. (what this connected mean? because i still got error on next question)

08-25 00:09:01.890: D/BaseGameActivity(11222): isGooglePlayServicesAvailable returned 0  
08-25 00:09:01.890: D/BaseGameActivity(11222): beginUserInitiatedSignIn: starting new sign-in flow.  
08-25 00:09:01.890: D/BaseGameActivity(11222): All clients now connected. Sign-in successful.  
08-25 00:09:01.890: D/BaseGameActivity(11222): All requested clients connected. Sign-in succeeded!  

3 . how do i use connect()? i have read and tried about gameClient and GameClientBuilder but i have no idea how to use that. when i tried run this code.

startActivityForResult(gameHelper.getGamesClient().getAllLeaderboardsIntent(), RC_UNUSED);  

i got this log.

08-25 00:09:05.660: E/AndroidRuntime(11222): java.lang.IllegalStateException: Not connected. Call connect() and wait for onConnected() to be called.

4 . to use leaderboard i know i must use code from google play store such as CgkIx****AIQAA. but i didn't found where i must put this code to load leaderboard.

sorry for long question, but i think if there is a sample that only for connect and either access achievement or leaderboard it will answer all my question. please don't tell me to see type-a-number sample, i did that and i need another sample code.

update, my snipped code

public class MainMenu extends Activity 
implements OnClickListener, GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener, GameHelperListener{

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main_menu);
  gameHelper = new GameHelper(this);
}

@Override
public void onClick(View v) {
  if(v.equals(loadData)) {
    if(gameHelper.isSignedIn()) {
      gameHelper.setup(this, GameHelper.CLIENT_GAMES, Scopes.GAMES);
      startActivityForResult(gameHelper.getGamesClient().getAllLeaderboardsIntent(), RC_UNUSED);
    }
  }
  else if(v.equals(loginButton)) {
    Intent googlePicker = AccountPicker.newChooseAccountIntent(null,null,new String[]{GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE},true,null,null,null,null) ;
    startActivityForResult(googlePicker, REQUEST_CODE_PICK_ACCOUNT);
  }
}

@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
  if(requestCode==REQUEST_CODE_RECOVER_PLAY_SERVICES) {
    if (resultCode == RESULT_CANCELED) {
      Toast.makeText(this, "Google Play Services must be installed.", Toast.LENGTH_SHORT).show();
      finish();
    }
    return;
  }
  else if(requestCode==REQUEST_CODE_PICK_ACCOUNT) {
    if (resultCode == RESULT_OK) {
      String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
      gameHelper.beginUserInitiatedSignIn();
    }
    else if (resultCode == RESULT_CANCELED) {
      Toast.makeText(this, "This application requires a Google account.", Toast.LENGTH_SHORT).show();
      finish();
    }
    return;
  }
  super.onActivityResult(requestCode, resultCode, data);
}

// this 2 methods not called, is this also because my code is wrong?
@Override
public void onSignInFailed() {
  Log.d("rush", "on sign in failed");
}

@Override
public void onSignInSucceeded() {
  Log.d("rush", "on sign in succeed");
}

}

回答1:


  1. Yes. Take a look at the BaseGameActivity source and see that it largely just wraps GameHelper. You can implement the calls to GameHelper yourself - in fact, you can probably copy some code directly from BaseGameActivity. I'm a bit confused, because it appears that your code is already using GameHelper. It looks like you are mixing GameHelper calls with BaseGameActivity calls. You cannot do this, and it will result in... errors like you are getting.

  2. The LogCat you see means that all of your clients are connected. The default call to GameHelper.setup() just requests the Games client. If you aren't using BaseGameActivity and want different clients, do:

    gameHelper = new GameHelper(this);
    gameHelper.setup(this, GameHelper.CLIENT_GAMES | GameHelper.CLIENT_PLUS);
    
  3. beginUserInitiatedSignIn() is an asynchronous method with a callback when it finishes. Are you running it that way? GameHelper.GameHelperListener is the interface to implement. If you are using gameHelper, make sure to register the callback. See the this in the setup call above? That's registering the callback (this is my main activity).

    As I said above, it looks like you are mixing GameHelper calls with BaseGameActivity calls. The GameHelper that is connected is the BaseGameActivity.mHelper instance, not any GameHelper you might have instantiated. Make sure that if you are using BaseGameActivity that you are not using GameHelper as well.

  4. If you want to display a single leaderboard, use the GamesClient.getLeaderboardIntent(string, int) or method to get the Intent. The string is the code you have (CgkIx****AIQAA).

    startActivityForResult(gameHelper.getGamesClient().getLeaderboardIntent(
            leaderboard_id, RC_UNUSED);
    

    Again, make sure you are using the correct getGamesClient() method, depending on if you are using BaseGameActivity or GameHelper directly.




回答2:


Here is basic information how to use GameHelper without BaseGameActivity:

https://developers.google.com/games/services/android/init#using_gamehelper_without_basegameactivity



来源:https://stackoverflow.com/questions/18421635/how-to-connect-to-google-play-service-and-load-leaderboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!