【问题标题】:How to trigger onActivityResult如何触发onActivityResult
【发布时间】:2017-01-12 03:43:45
【问题描述】:

我正在实现 buttonclicker 的修改版本 - 多人安卓游戏的通用示例。

玩家可以选择进入“快速游戏”,其中寻求多人游戏的玩家随机匹配。我希望玩家等待 30 秒,如果没有找到匹配的玩家,则要求玩家玩电脑。

相关代码:

    @Override
        public void onActivityResult(int requestCode, int responseCode,
                                     Intent intent) {
            super.onActivityResult(requestCode, responseCode, intent);
    
            switch (requestCode) {
                ...
    ...
                case RC_WAITING_ROOM:
                    if (responseCode == Activity.RESULT_OK) {
                        startGame(true);
                        mSYGameOn = -1;
                    } else if (responseCode == GamesActivityResultCodes.RESULT_LEFT_ROOM) {
                        leaveRoom();
                    } else if (responseCode == Activity.RESULT_CANCELED) {
                        // The code that I want to execute
                        leaveRoom();
                        gotoMyPlayWithComputerCode()
                    }
                    break;
            }
            super.onActivityResult(requestCode, responseCode, intent);
        }

我认为实现这一点的最佳方法是在 30 秒结束时触发 onActivityResult,然后执行我的自定义代码。

快游戏调用者:

    @JavascriptInterface
    public void multiButtonFunction (String typeButton) {
        Intent intent;
        switch (typeButton) {
...
...
            case "button_quick_game":
                // user wants to play against a random opponent right now
                startQuickGame();
                break;
        }
     }

现在 QuickGame 的代码如下。代码的Handler部分是我添加的触发onActivityResult,这将反过来退出快速游戏屏幕,然后将进入我的自定义代码。:

 void startQuickGame() {
        final int MIN_OPPONENTS = 1, MAX_OPPONENTS = 1;
        Bundle autoMatchCriteria = RoomConfig.createAutoMatchCriteria(MIN_OPPONENTS,
                MAX_OPPONENTS, 0);
        RoomConfig.Builder rtmConfigBuilder = RoomConfig.builder(this);
        rtmConfigBuilder.setMessageReceivedListener(this);
        rtmConfigBuilder.setRoomStatusUpdateListener(this);
        rtmConfigBuilder.setAutoMatchCriteria(autoMatchCriteria);
        keepScreenOn();
        resetGameVars();
        mSYGameOn=1;
        Games.RealTimeMultiplayer.create(mGoogleApiClient, rtmConfigBuilder.build());
        Handler handler = new Handler();
        handler.postDelayed(new Runnable()
        {
            @Override
            public void run() {
                if (mSYGameOn>0) {
                    setResult(Activity.RESULT_CANCELED, getIntent());
                }
            }
        }, 10000);
    }

可以看出,我尝试了 setResult 但这不起作用,因为 onActivityResult 没有通过它触发。我做错了什么还是有其他方法可以做同样的事情。

任何帮助将不胜感激。

谢谢

【问题讨论】:

    标签: android google-play-games


    【解决方案1】:

    一个非常简单的答案。处理程序代码应为:

    handler.postDelayed(new Runnable()
            {
                @Override
                public void run() {
                    if (mSYGameOn>0) {
                        finishActivity(RC_WAITING_ROOM);
                    }
                }
            }, 30000);
    

    这会触发onActivityResult() 并将房间视为已取消,退出等候室。

    文档here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-07
      • 1970-01-01
      • 1970-01-01
      • 2014-10-24
      • 2017-06-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多