이 가이드에서는 Android 스튜디오 프로젝트에서 Friends API를 사용하는 방법을 설명합니다.
친구 로드
현재 사용자와 친구인 플레이어 목록을 게임에서 가져와서 표시할 수 있습니다. 사용자는 친구 목록에 액세스할 수 있는 게임을 제어할 수 있습니다. 친구 목록을 가져올 때는 권한이 필요한 경우를 처리해야 합니다. 이러한 작업은 모두 API에 캡슐화되므로 액세스 권한을 요청하고 이후에 친구 목록을 사용하는 작업이 간단해집니다. 친구 목록을 로드하려면 다음 단계를 따르세요.
- Task객체를 반환하는 비동기 호출인- PlayersClient.loadFriends()메서드를 호출합니다.
- 호출이 성공하면(사용자가 이미 친구 목록 액세스 권한을 부여한 경우) 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와는 별개로 자체 플레이어 이름 개념이 있으면 추가 컨텍스트를 위해 친구 초대에 포함할 수 있도록 프로필 뷰에 이를 전달할 수 있습니다.
다른 플레이어의 프로필을 표시하려면 다음 단계를 따르세요.
- Task객체를 반환하는 비동기 호출인- PlayersClient.getCompareProfileIntent()메서드를 호출합니다.
- 호출이 성공하면 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 게임즈에서는 게임 내에서 친구 초대를 보내는 플레이어의 닉네임을 '<your-game-name>'에서 '<game-specific-name>'으로 설정할 수 있습니다. Play 게임즈는 자동으로 'from <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 authenticated.
//   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);
          // ...
        }});
