简体   繁体   English

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

[英]How to launch specific activity from GooglePlayGames invitation

I'm trying to create a new android application that is comprised of multiple mini-games. 我正在尝试创建一个由多个迷你游戏组成的新android应用程序。 The launcher activity extends BaseGameActivity and has a sign-in button and a ListView containing all the possible games that can be played. 启动器活动扩展了BaseGameActivity,并具有一个登录按钮和一个ListView,其中包含所有可能玩的游戏。

Inside of a mini-game activity (also extends BaseGameActivity), how can I get it to create a notification which will launch a specific Activity? 在迷你游戏活动(还扩展了BaseGameActivity)中,如何获取它来创建将启动特定活动的通知? Currently, when I call invitePlayersToGame, the invitation that gets sent is for the full application (Mini-Games) and not the individual game (specific dice game). 当前,当我致电invokePlayersToGame时,发送的邀请是针对完整应用程序(迷你游戏)的,而不是针对单个游戏(特定的骰子游戏)的。

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

Is there a way to get the notification to generate with a specific message? 有没有一种方法可以使通知与特定消息一起生成? Is there a way to get notification to open directly to the mini-game activity without going to the main launcher activity first? 有没有办法获得通知以直接打开迷你游戏活动而无需先进入主启动器活动?

Any help is appreciated. 任何帮助表示赞赏。 Thanks! 谢谢!

You can send sendReliableMessage for method handshaking. 您可以发送sendReliableMessage进行方法握手。

First enter a room (quickgame or send invite). 首先进入一个房间(快速游戏或发送邀请)。

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

After this, you can send model class (includes what you need). 之后,您可以发送模型类(包括所需的内容)。

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

finally you can reach the data on other devices: 最终您可以在其他设备上访问数据:

 @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:
   ....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM