Android 玩游戏 - 实时多人游戏 - 邀请无效

Android games play - realtime multiplayer - Invites not working

最近几天我一直被这个问题困扰。这是我的第一个应用程序,所以我可能犯了一个我无法弄清楚的愚蠢错误。

我正在制作一个简单的 webView 多人实时游戏,使用 android 玩游戏并重新设计 android sample's ButtonClicker2000 游戏。但是,当我邀请玩家时,我看不到任何邀请。邀请和创建房间显然是成功的(显示正确logs/toasts)。但是,当我查看待处理的邀请(邀请收件箱)时,onActivityResult 的响应是 0 而不是 Activity.RESULT_OK 或 -1.

如果代码太长,请见谅。只是想我会粘贴所有相关部分

相关代码为: 这里 invitePlayersseeInvites 是相关案例。

@JavascriptInterface
public void multiButtonFunction (String typeButton) {
    Intent intent;
    switch (typeButton) {
        case "signin":
            // start the sign-in flow
            if (!verifySampleSetup(this, R.string.app_id)) {
                Log.w(TAG, "*** Warning: setup problems detected. Sign in may not work!");
            }
            Toast.makeText(getApplicationContext(), "Sign-in button clicked0", Toast.LENGTH_LONG).show();
            mSignInClicked = true;
            mGoogleApiClient.connect();
            break;
        case "signout":
            // start the sign-out flow
            Toast.makeText(getApplicationContext(), "Sign-Out button clicked0", Toast.LENGTH_LONG).show();
            mSignInClicked = false;
            Games.signOut(mGoogleApiClient);
            mGoogleApiClient.disconnect();
            break;
        case "invitePlayers":
            // show list of invitable players
            intent = Games.RealTimeMultiplayer.getSelectOpponentsIntent(mGoogleApiClient, 1, 3);
            switchToScreen(0);
            startActivityForResult(intent, RC_SELECT_PLAYERS);
            break;
        case "seeInvites":
            // show list of pending invitations
            intent = Games.Invitations.getInvitationInboxIntent(mGoogleApiClient);
            startActivityForResult(intent, RC_INVITATION_INBOX);
        case "acceptInvites":
            // user wants to accept the invitation shown on the invitation popup
            // (the one we got through the OnInvitationReceivedListener).
            acceptInviteToRoom(mIncomingInvitationId);
            mIncomingInvitationId = null;
            break;
        case "quickGame":
            // user wants to play against a random opponent right now
            startQuickGame();
            break;
    }
}

当我收到回复时:(RC_SELECT_PLAYERS 是相关的 switch case)

   @Override
    public void onActivityResult(int requestCode, int responseCode,
                                 Intent intent) {
        super.onActivityResult(requestCode, responseCode, intent);

        switch (requestCode) {
            case RC_SELECT_PLAYERS:
                // we got the result from the "select players" UI -- ready to create the room
                handleSelectPlayersResult(responseCode, intent);
                break;
            case RC_INVITATION_INBOX:
                // we got the result from the "select invitation" UI (invitation inbox). We're
                // ready to accept the selected invitation:
                handleInvitationInboxResult(responseCode, intent);
                break;
            case RC_WAITING_ROOM:
                // we got the result from the "waiting room" UI.
                if (responseCode == Activity.RESULT_OK) {
                    startGame(true);
                } else if (responseCode == GamesActivityResultCodes.RESULT_LEFT_ROOM) {
                    leaveRoom();
                } else if (responseCode == Activity.RESULT_CANCELED) {
                    leaveRoom();
                }
                break;
            case RC_SIGN_IN:
                Log.d(TAG, "onActivityResult with requestCode == RC_SIGN_IN, responseCode="+ responseCode + ", intent=" + intent);
                Toast.makeText(getApplicationContext(), "onActivityResult with requestCode == RC_SIGN_IN, responseCode="+ responseCode + ", intent=" + intent, Toast.LENGTH_LONG).show();
                mSignInClicked = false;
                mResolvingConnectionFailure = false;
                if (responseCode == RESULT_OK) {
                    mGoogleApiClient.connect();
                } else {
                    //BaseGameUtils.showActivityResultError(this,requestCode,responseCode, R.string.signin_other_error);
                    (new AlertDialog.Builder(this))
                            .setTitle(responseCode)
                            .setMessage(R.string.signin_other_error)
                            .setNeutralButton(android.R.string.ok, null)
                            .create();
                }
                break;
        }
        super.onActivityResult(requestCode, responseCode, intent);
    }

最后是处理邀请的函数:

private void handleSelectPlayersResult(int response, Intent data) {
    if (response != Activity.RESULT_OK) {
        Toast.makeText(getApplicationContext(),  "*** select players UI cancelled, " + response, Toast.LENGTH_LONG).show();
        switchToMainScreen();
        return;
    }
    Toast.makeText(getApplicationContext(),  "Select players UI succeeded.", Toast.LENGTH_LONG).show();

    // get the invitee list
    final ArrayList<String> invitees = data.getStringArrayListExtra(Games.EXTRA_PLAYER_IDS);
    Toast.makeText(getApplicationContext(),"Invitee count: " + invitees.size(), Toast.LENGTH_LONG).show();

    // get the automatch criteria
    Bundle autoMatchCriteria = null;
    int minAutoMatchPlayers = data.getIntExtra(Multiplayer.EXTRA_MIN_AUTOMATCH_PLAYERS, 0);
    int maxAutoMatchPlayers = data.getIntExtra(Multiplayer.EXTRA_MAX_AUTOMATCH_PLAYERS, 0);
    if (minAutoMatchPlayers > 0 || maxAutoMatchPlayers > 0) {
        autoMatchCriteria = RoomConfig.createAutoMatchCriteria(
                minAutoMatchPlayers, maxAutoMatchPlayers, 0);
        Toast.makeText(getApplicationContext(),"Automatch criteria: " + autoMatchCriteria, Toast.LENGTH_LONG).show();
    }

    // create the room
    Toast.makeText(getApplicationContext(), "Creating room...", Toast.LENGTH_LONG).show();
    RoomConfig.Builder rtmConfigBuilder = RoomConfig.builder(this);
    rtmConfigBuilder.addPlayersToInvite(invitees);
    rtmConfigBuilder.setMessageReceivedListener(this);
    rtmConfigBuilder.setRoomStatusUpdateListener(this);
    if (autoMatchCriteria != null) {
        rtmConfigBuilder.setAutoMatchCriteria(autoMatchCriteria);
    }
    //switchToScreen(R.id.screen_wait);
    keepScreenOn();
    resetGameVars();
    Games.RealTimeMultiplayer.create(mGoogleApiClient, rtmConfigBuilder.build());
    Toast.makeText(getApplicationContext(), "Room created, waiting for it to be ready...", Toast.LENGTH_LONG).show();
}
// Handle the result of the invitation inbox UI, where the player can pick an invitation
// to accept. We react by accepting the selected invitation, if any.
private void handleInvitationInboxResult(int response, Intent data) {
    if (response != Activity.RESULT_OK) {
        Toast.makeText(getApplicationContext(),  "*** invitation inbox UI cancelled, " + response, Toast.LENGTH_LONG).show();
        switchToMainScreen();
        return;
    }
    Toast.makeText(getApplicationContext(), "Invitation inbox UI succeeded.", Toast.LENGTH_LONG).show();
    Invitation inv = data.getExtras().getParcelable(Multiplayer.EXTRA_INVITATION);

    // accept invitation
    acceptInviteToRoom(inv.getInvitationId());
}
// Accept the given invitation.
void acceptInviteToRoom(String invId) {
    // accept the invitation
    Toast.makeText(getApplicationContext(), "Accepting invitation: " + invId, Toast.LENGTH_LONG).show();
    RoomConfig.Builder roomConfigBuilder = RoomConfig.builder(this);
    roomConfigBuilder.setInvitationIdToAccept(invId)
            .setMessageReceivedListener(this)
            .setRoomStatusUpdateListener(this);
    //switchToScreen(R.id.screen_wait);
    keepScreenOn();
    resetGameVars();
    Games.RealTimeMultiplayer.join(mGoogleApiClient, roomConfigBuilder.build());
}

嗯,错误是我没有在 Google 开发人员控制台中启用多人游戏。 Off 和 On 开关的颜色差别很大,我以为我把它打开了,但它是关闭的 p-)