我已经导入了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");
}
}
gameHelper = new GameHelper(this);
之后立即调用gameHelper.setup()
。您对设置的调用非常错误 - 您只能将整数标志Client_NONE、Client_GAMES、Client_PLUS、CLient_APPSTATE或Client_ALL(或前4个的某些组合)传递给它。