我在最近几天遇到了这个问题。这是我的第一个应用程序,所以我可能犯了一个我无法弄清楚的愚蠢错误。
我正在制作一个简单的webView多人实时游戏,使用Android玩游戏和重新设计android sample's ButtonClicker2000游戏。但是,当我邀请玩家时,我无法看到任何邀请。邀请和房间创建显然是成功的(显示所有正确的日志/祝酒词)。但是,当我查看待处理的邀请(邀请收件箱)时,onActivityResult
的响应为0而不是Activity.RESULT_OK
或-1。
如果代码太长,请抱歉。只是想到我会粘贴所有相关零件
相关代码是:
此处invitePlayers
和seeInvites
是相关案例。
@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
是相关的开关案例)
@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());
}
答案 0 :(得分:0)
错误的是我没有在Google Console中为开发人员启用多人游戏。 Off和On开关的颜色之间的差异是显着的,我认为我已将它打开,但它已关闭p - )