类似的问题对于答案来说是微不足道的。我正在使用最新的Google Play游戏服务apk 'com.google.android.gms:play-services:7.0.0'
,我实施了onInvitationReceived()
和onConnected()
。
然而,当被邀请者接受游戏中的游戏邀请时,onInvitationReceived()
似乎无法被调用。虽然我相当肯定onConnected()被调用,无论玩家在mGoogleClient.connect()
之后通过回调连接什么,看起来邀请被删除或因为玩家没有重定向到游戏屏幕我指定Bundle是否包含邀请(我假设在关闭应用程序时调用了这个邀请,但无论如何,只显示邀请的状态栏通知)。
对此有任何见解将不胜感激。以下是相关方法:
onConnected()来自一个扩展AndroidGame的类,AndroidGame扩展了Activity(我已经在两个类中尝试过它,现在我只是在AndroidGame的子类中重写了它):
@Override
public void onConnected(Bundle connectionHint) {
// Connected to Google Play services!
// The good stuff goes here.
Games.Invitations.registerInvitationListener(mGoogleClient, Network.getInstance().mListener);
if (connectionHint != null) {
Invitation inv =
connectionHint.getParcelable(Multiplayer.EXTRA_INVITATION);
if (inv != null) {
// accept invitation
RoomConfig.Builder roomConfigBuilder = RoomConfig.builder(Network.getInstance());
roomConfigBuilder.setMessageReceivedListener(Network.getInstance());
roomConfigBuilder.setRoomStatusUpdateListener(Network.getInstance());
roomConfigBuilder.setInvitationIdToAccept(inv.getInvitationId());
RoomConfig roomConfig = roomConfigBuilder.build();
Games.RealTimeMultiplayer.join(mGoogleClient, roomConfig);
// prevent screen from sleeping during handshake
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
// go to game screen
setScreen(new CharSelectScreen(this));
}
}
}
onInvitationReceived来自我的Network类,它基本上是一个处理其他侦听器的单例。 onInvitationReceived()在网络类和匿名内部类中都被覆盖,只是为了覆盖我的资产:
@Override
public void onInvitationReceived(Invitation invitation) {
Intent intent = Games.Invitations.getInvitationInboxIntent(game.getGoogleClient());
game.startActivityForResult(intent, RC_INVITATION_INBOX);
mIncomingInvitationId = invitation.getInvitationId();
AlertDialog.Builder builder = new AlertDialog.Builder(game.getApplicationContext());
builder.setMessage("Join Game?")
.setTitle("Bit Game Invitation!");
builder.setPositiveButton("Join", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if(!game.getGoogleClient().isConnected()) {
game.getGoogleClient().connect();
}
// User clicked OK button
Bundle am = RoomConfig.createAutoMatchCriteria(1, 3, 0);
RoomConfig.Builder roomConfigBuilder = RoomConfig.builder(Network.getInstance());
roomConfigBuilder.setMessageReceivedListener(Network.getInstance());
roomConfigBuilder.setRoomStatusUpdateListener(Network.getInstance());
roomConfigBuilder.setAutoMatchCriteria(am);
roomConfigBuilder.setInvitationIdToAccept(mIncomingInvitationId);
RoomConfig roomConfig = roomConfigBuilder.build();
Games.RealTimeMultiplayer.join(game.getGoogleClient(), roomConfig);
// prevent screen from sleeping during handshake
game.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
dialog.dismiss();
// go to game screen
game.setScreen(new CharSelectScreen(game));
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
答案 0 :(得分:1)
我解决了这个问题,简短的回答是我没有像我应该的那样严格遵循Android Realtime Multiplayer食谱(指南)。
这些指南要求您在启动应用时自动轻松访问Google Play登录,或者我最好想象一下,还有一个登录按钮。当我添加此功能时,我的回调onConnected()
代码有效,邀请允许用户使用默认等候室弹出窗口进入指定的游戏屏幕(显示实施here)。我仍在调试onInvitationReceived()
回调,但我怀疑当游戏开启时收到邀请,游戏崩溃意味着我的实施有问题。
总而言之,我认为这对我来说有点误解,文档非常直观。感谢您的帮助,我希望这将有助于未来的Google Play游戏服务开发人员。