이 가이드의 단계에 따라 Java 게임 코드에서 Friends API를 구현하세요.
친구 로드
현재 사용자와 친구인 플레이어 목록을 게임에서 가져와서 표시할 수 있습니다. 사용자는 친구 목록에 액세스할 수 있는 게임을 제어할 수 있습니다. 친구 목록을 가져올 때는 권한이 필요한 경우를 처리해야 합니다. 이러한 작업은 모두 API에 캡슐화되므로 액세스 권한을 요청하고 이후에 친구 목록을 사용하는 작업이 간단해집니다. 친구 목록을 로드하려면 다음 단계를 따르세요.
- 먼저
PlayersClient.loadFriends()
드림 메서드를 제공합니다. 이는Task
객체. - 통화가 성공한 경우 (사용자가 이미 친구에게 액세스 권한을 부여함)
Google Play 게임즈 서비스는 애플리케이션의
PlayerBuffer
드림 사용자의 친구를 나타냅니다. 플레이어가 친구 목록 액세스 권한을 부여해야 하는 경우 호출은
FriendsResolutionRequiredException
과 함께 실패합니다. 아직 대화상자가 표시되지 않습니다.- 이 예외에는 플레이어에게 동의를 요청하는 대화상자를 트리거하는
Intent
가 포함됩니다. 이Intent
를 즉시 실행하여 동의 대화상자를 열 수 있습니다. 이Intent
는 한 번만 사용할 수 있습니다. Intent
활동의 결과가Activity.RESULT_OK
인 경우 동의가 부여된 것입니다.loadFriends()
를 다시 호출하여 친구 목록을 반환합니다. 결과가Activity.RESULT_CANCELLED
이면 사용자가 동의하지 않은 것이며loadFriends()
는 계속FriendsResolutionRequiredException
을 반환합니다.
- 이 예외에는 플레이어에게 동의를 요청하는 대화상자를 트리거하는
다음 코드는 친구 목록 로드를 구현하는 방법을 보여줍니다.
// Attempt loading friends.
// Register a success listener to handle the successfully loaded friends list.
// Register a failure listener to handle asking for permission to access the list.
PlayGames.getPlayersClient(this)
.loadFriends(PAGE_SIZE, /* forceReload= */ false)
.addOnSuccessListener(
new OnSuccessListener<AnnotatedData<PlayerBuffer>>() {
@Override
public void onSuccess(AnnotatedData<PlayerBuffer> data) {
PlayerBuffer playerBuffer = data.get();
// ...
})
.addOnFailureListener(
exception -> {
if (exception instanceof FriendsResolutionRequiredException) {
PendingIntent pendingIntent =
((FriendsResolutionRequiredException) task.getException())
.getResolution();
parentActivity.startIntentSenderForResult(
pendingIntent.getIntentSender(),
/* requestCode */ SHOW_SHARING_FRIENDS_CONSENT,
/* fillInIntent */ null,
/* flagsMask */ 0,
/* flagsValues */ 0,
/* extraFlags */ 0,
/* options */ null);
}
});
return;
}
다음 코드는 동의 요청 결과를 처리하는 방법을 보여줍니다.
/** Handle the activity result from the request for consent. */
@Override
public void onActivityResult(int requestCode, int result, Intent data) {
if (requestCode == SHOW_SHARING_FRIENDS_CONSENT) {
if (result == Activity.RESULT_OK) {
// We got consent from the user to access their friends. Retry loading the friends
callLoadFriends();
} else {
// User did not grant consent.
}
}
}
다른 플레이어의 프로필 보기
게임 내에서 다른 플레이어의 Play 게임즈 프로필 뷰를 표시할 수 있습니다. 이 뷰를 통해 플레이어는 보고 있는 플레이어의 친구 초대를 보내고 수락할 수 있습니다. 이 뷰에는 친구 목록 액세스 권한이 필요하지 않습니다. 또한 게임에 Play 게임즈 게이머 ID와는 별개로 자체 플레이어 이름 개념이 있으면 추가 컨텍스트를 위해 친구 초대에 포함할 수 있도록 프로필 뷰에 이를 전달할 수 있습니다.
다른 플레이어의 프로필을 표시하려면 다음 단계를 따르세요.
- 먼저
PlayersClient.getCompareProfileIntent()
드림 메서드를 제공합니다. 이는Task
객체. - 호출이 성공하면 Google Play 게임즈 서비스는 사용자가 다른 앱과 비교할 수 있는 화면이 표시됩니다. 지정할 수 있습니다.
- 이전 단계의
Intent
를 사용하여 활동을 시작합니다.
// Retrieve and launch an Intent to show a player profile within the game.
PlayGames.getPlayersClient(this)
.getCompareProfileIntent(otherPlayerId)
.addOnSuccessListener(new OnSuccessListener<Intent>() {
@Override
public void onSuccess(Intent intent) {
startActivityForResult(intent, RC_SHOW_PROFILE);
// ...
}});
게임에 플레이어의 고유 이름이 있는 경우 API 호출에 추가할 수 있습니다. 이렇게 하면 Play 게임즈에서 친구를 보내는 플레이어의 닉네임을 설정할 수 있습니다. 게임 내에서 '<game-specific-name>'에 초대 최저가: <게임 이름> 드림 (Play 게임즈는 자동으로 ' <your-game-name>"):
// Show a player profile within the game, with additional hints containing the
// game-specific names for both players.
// - otherPlayerId is the Play Games playerId of the player to view.
// - otherPlayerInGameName is the game-specific name of the player being viewed.
// - currentPlayerInGameName is the game-specific name of the player who is signed
// in. Hence if the player sends an invitation to the profile they are viewing,
// their game-specific name can be included.
PlayGames.PlayersClient(this)
.getCompareProfileIntentWithAlternativeNameHints(otherPlayerId, otherPlayerInGameName, currentPlayerInGameName)
.addOnSuccessListener(new OnSuccessListener<Intent>() {
@Override
public void onSuccess(Intent intent) {
startActivityForResult(intent, RC_SHOW_PROFILE);
// ...
}});