如何从GooglePlayGames邀请中启动特定活动

时间:2014-08-24 01:09:57

标签: java android google-play-games

我正在尝试创建一个由多个迷你游戏组成的新Android应用程序。启动器活动扩展了BaseGameActivity并具有登录按钮和ListView,其中包含可以播放的所有可能游戏。

在迷你游戏活动内部(也扩展了BaseGameActivity),如何让它创建一个启动特定活动的通知?目前,当我调用invitePlayersToGame时,发送的邀请是针对完整应用程序(迷你游戏)而不是单个游戏(特定骰子游戏)。

public void invitePlayersToGame(View pView) {
    Intent intent = Games.RealTimeMultiplayer.getSelectOpponentsIntent(getApiClient(), 1, 1);
    intent.putExtra("gameName", "Patman Yahtzee");
    startActivityForResult(intent, RC_SELECT_PLAYERS);
}

有没有办法通过特定邮件生成通知?有没有办法让通知直接打开迷你游戏活动而不先进入主启动器活动?

感谢任何帮助。谢谢!

1 个答案:

答案 0 :(得分:0)

您可以发送sendReliableMessage进行方法握手。

首先进入房间(快速游戏或发送邀请)。

public void openInvitationIntent() {
    // launch the player selection screen
    // minimum: 1 other player; maximum: 1 other players
    Intent intent = Games.RealTimeMultiplayer.getSelectOpponentsIntent(mGoogleApiClient, 1, 1);
    startActivityForResult(intent, RC_SELECT_PLAYERS);
}

onConnected:

   @Override
public void onConnected(Bundle connectionHint) {
    // QuickGame
    if (mGameMode == 1) {
        Log.d(TAG, "onConnected() called. Sign in successful!");
        Log.d(TAG, "Sign-in succeeded.");
        startQuickGame();

        // register listener so we are notified if we receive an invitation to play
        // while we are in the game
        if (connectionHint != null) {
            Log.d(TAG, "onConnected: connection hint provided. Checking for invite.");
            Invitation inv = connectionHint.getParcelable(Multiplayer.EXTRA_INVITATION);
            if (inv != null && inv.getInvitationId() != null) {
                // retrieve and cache the invitation ID
                Log.d(TAG, "onConnected: connection hint has a room invite!");
                acceptInviteToRoom(inv.getInvitationId());
                return;
            }
        }
    }
    // Send request
    else if (mGameMode == 0) {
        // request code for the "select players" UI
        // can be any number as long as it's unique
        invitationInbox();
    }
    // request accepted
    else {
        mIncomingInvitationId = getIntent().getExtras().getString(AppConstants.RC_INVITATION_ID);
        RoomConfig.Builder roomConfigBuilder = makeBasicRoomConfigBuilder();
        roomConfigBuilder.setInvitationIdToAccept(mIncomingInvitationId);
        Games.RealTimeMultiplayer.join(mGoogleApiClient, roomConfigBuilder.build());

        // prevent screen from sleeping during handshake
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }
}

在此之后,您可以发送模型类(包括您需要的内容)。

 private void broadcastMessage(ModelGameRecievedMessage broadcastedMessage, boolean isFinal) {
    try {
        if ( mParticipants != null && broadcastedMessage != null) {
            byte[] bytes = Utils.serialize(broadcastedMessage);
            // Send to every other participant.
            for (Participant p : mParticipants) {
                if (p.getParticipantId().equals(mMyId)) {
                    continue;
                }
                if (p.getStatus() != Participant.STATUS_JOINED) {
                    continue;
                }
                if (mRoomId != null) {
                    if (isFinal) {
                        // final score notification must be sent via reliable broadcastedMessage
                        Games.RealTimeMultiplayer.sendReliableMessage(mGoogleApiClient, null, bytes,
                                mRoomId, p.getParticipantId());
                    } else {
                        // it's an interim score notification, so we can use unreliable
                        Games.RealTimeMultiplayer.sendUnreliableMessage(mGoogleApiClient, bytes,
                                mRoomId, p.getParticipantId());
                    }
                }
            }
            Logy.l("broadcastedMessage.getMessageTypeId(); " + broadcastedMessage.getMessageTypeId());
            Logy.l("broadcastedMessage.getMessage(); " + broadcastedMessage.getMessage());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

最后,您可以访问其他设备上的数据:

 @Override
public void onRealTimeMessageReceived(RealTimeMessage rtm) {

    byte[] bufy = rtm.getMessageData();
    ModelGameRecievedMessage recievedMessage = null;
    try {
        recievedMessage = (ModelGameRecievedMessage) Utils.deserialize(bufy);
        Logy.l("recievedMessage.getMessageTypeId(); " + recievedMessage.getMessageTypeId());
        Logy.l("recievedMessage.getMessage(); " + recievedMessage.getMessage());
    } catch (Exception e) {
        Logy.e("Exception onRealTimeMessageReceived deserialize:  " + e);
    }
    switch (recievedMessage.getMessageTypeId()) {
        case AppConstants.RC_MULTI_START_TIMEMILIS_MULTIPLAYER:
   ....