我按照 google 示例使用 google play 游戏服务制作了一个简单的示例游戏应用程序。
在代码中,我使用GameClient.getSelectPayersIntent()
类似于示例的方法启动了玩家选择屏幕。
intent = getGamesClient().getSelectPlayersIntent(1, 3);
showScreen(Screen.WAITING);
startActivityForResult(intent, RC_SELECT_PLAYERS);
玩家选择活动返回结果数据,但它不包括GamesClient.EXTRA_PLAYERS
, GamesClient.EXTRA_MIN_AUTOMATCH_PLAYERS
, GamesClient.EXTRA_MAX_AUTOMATCH_PLAYERS
extras,即使我选择了两个自动匹配玩家。
当我调试它时,它的额外地图中只包含com.google.android.gms.games.MAX_SELECTION
, com.google.android.gms.games.MIN_SELECTION
extras 。
在getSelectPlayersIntent的 javadoc 中,结果应该包括选择的玩家信息。
我需要为此功能进行一些设置吗?
这是我的onActivityResult
代码。我只是从样本中复制它。
@Override
protected void onActivityResult(int requestCode, int responseCode, Intent data) {
super.onActivityResult(requestCode, responseCode, data);
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;
... other stuffs
private void handleSelectPlayersResult(int response, Intent data) {
if (response != Activity.RESULT_OK) {
Log.w(TAG, "*** select players UI cancelled, " + response);
showScreen(Screen.MAIN);
return;
}
Log.d(TAG, "Select players UI succeeded.");
// get the invitee list
final ArrayList<String> invitees = data.getStringArrayListExtra(GamesClient.EXTRA_PLAYERS);
if (invitees != null) {
Log.d(TAG, "Invitee count: " + invitees.size());
}
// get the automatch criteria
Bundle autoMatchCriteria = null;
int minAutoMatchPlayers = data.getIntExtra(GamesClient.EXTRA_MIN_AUTOMATCH_PLAYERS, 0);
int maxAutoMatchPlayers = data.getIntExtra(GamesClient.EXTRA_MAX_AUTOMATCH_PLAYERS, 0);
if (minAutoMatchPlayers > 0 || maxAutoMatchPlayers > 0) {
autoMatchCriteria = RoomConfig.createAutoMatchCriteria(
minAutoMatchPlayers, maxAutoMatchPlayers, 0);
Log.d(TAG, "Automatch criteria: " + autoMatchCriteria);
}
// create the room
Log.d(TAG, "Creating room...");
RoomConfig.Builder rtmConfigBuilder = RoomConfig.builder(this);
if (invitees != null) {
rtmConfigBuilder.addPlayersToInvite(invitees);
}
rtmConfigBuilder.setMessageReceivedListener(this);
rtmConfigBuilder.setRoomStatusUpdateListener(this);
if (autoMatchCriteria != null) {
rtmConfigBuilder.setAutoMatchCriteria(autoMatchCriteria);
}
showScreen(Screen.WAITING);
keepScreenOn();
getGamesClient().createRoom(rtmConfigBuilder.build());
Log.d(TAG, "Room created, waiting for it to be ready...");
}
更新
哎呀,是我的错。我使用了发送意图,而不是接收数据意图。