如何连接Google Play服务并加载排行榜。

3
我想将我的游戏与Google Play服务连接。我已经阅读了Android开发者文档,并尝试了Type-a-Number示例,但仍然无法加载排行榜。
我已经导入了baseGameUtils,但我使用的是AndEngine,所以我没有使用来自Google的extends BaseGameActivity。
我现在拥有的内容:
- GooglePlayServicesUtil.isGooglePlayServicesAvailable(this)返回成功
- startActivityForResult(pickAccountIntent,REQUEST_CODE_PICK_ACCOUNT); 正常工作,我从onActivityResult(..)中获得了我的帐户名称;
- 我已经在我的清单文件中添加了这个。
<meta-data android:name="com.google.android.gms.games.APP_ID"
        android:value="@string/app_id" />

我的问题是:
1. 我是否可以在不扩展 BaseGameActivity 的情况下使用 Google Play 服务?
2. 如果我在获取账户名称后使用 gameHelper.beginUserInitiatedSignIn();,我会在 log cat 上看到这个“connected”信息(这个“connected”是什么意思?因为我还是在下一个问题上遇到了错误)。

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. 如何使用connect()方法?我已经阅读并尝试了gameClient和GameClientBuilder,但我不知道该如何使用它。当我尝试运行以下代码时:

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

我收到了这个日志文件。
08-25 00:09:05.660: E/AndroidRuntime(11222): java.lang.IllegalStateException: Not connected. Call connect() and wait for onConnected() to be called.

4. 如果要使用排行榜,我知道我必须从Google Play商店中获取代码,例如CgkIx****AIQAA。但我没有找到应该放置这个代码的位置来加载排行榜。

抱歉问题比较长,但如果有一个仅用于连接和访问成就或排行榜的示例代码,它将回答所有我的问题。请不要告诉我去看“打字游戏”示例,我已经看过了,我需要另一个示例代码。

更新,我的代码片段:

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

}
2个回答

2
  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.


  1. 我已经更新了我的问题并附上了代码。
  2. "client"是什么意思?是用于多人游戏吗?
  3. 我也考虑过设置。我在beginUserInitiatedSignin()之前放置了以下代码:gameHelper.setup(this, GameHelper.CLIENT_GAMES, Scopes.GAMES); 但没有任何反应。
  4. 我已经尝试了所有我能做的事情,但仍然找不到如何正确使用GamesClient()。
- gondai yosuke
  1. 是的。
  2. 在这种情况下,客户端是API客户端。您想要使用Google Play游戏服务客户端来实现成就和排行榜。多人游戏还需要Plus客户端。
  3. 如果您只使用游戏客户端,请在gameHelper = new GameHelper(this);之后立即调用gameHelper.setup()。您对设置的调用非常错误 - 您只能将整数标志Client_NONE、Client_GAMES、Client_PLUS、CLient_APPSTATE或Client_ALL(或前4个的某些组合)传递给它。
- Jeremy Scoggins
我注意到你的意思是我将gameHelper与baseGameActivity混合在一起。目前,我已按照你所说的放置了代码。但仍然没有调用监听器。现在,当我运行BeginUserInitiatedSignIn()时,一切都进行得很顺利,但选择我的ID后,my gameHelper.isSignedIn()仍然为false。当然,有什么建议吗? - gondai yosuke
你现在可能遇到了密钥问题。你是否在Play控制台中发布了你的Google Play游戏服务设置?你确定你用来签署APK的密钥与你用来创建GPGS项目的密钥匹配吗?在stackoverflow上搜索一下,你会发现有很多关于GPGS登录失败的讨论。就我个人而言,即使一切都设置正确,我的调试APK有时也可能无法正确登录。 - Jeremy Scoggins

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接