整合 Friends API (Java)

請按照本指南中的步驟,在 Java 遊戲程式碼中實作 Friends API。

載入好友

您可以擷取並顯示 (遊戲中) 好友的玩家名單 與目前使用者通訊使用者可以控制 存取好友名單。擷取好友名單時,您必須處理 應用程式取得權限這些字元都會在 API 中封裝 要求存取權,然後輕鬆地透過好友名單 工作。如要載入好友名單,請按照下列步驟操作:

  1. PlayersClient.loadFriends()敬上 方法,此方法會傳回 Task 物件。
  2. 如果通話成功 (使用者已將好友存取權授予好友) 清單),Google Play 遊戲服務會傳回已加上註解的註解 PlayerBuffer敬上 代表使用者的好友
  3. 如果玩家必須授予好友名單的存取權,通話就會失敗,並顯示 換 FriendsResolutionRequiredException。 此時尚未顯示任何對話方塊。

    1. 這個例外狀況包含 Intent,可觸發對話方塊來要求 播放器。您可以立即啟動這個「Intent」,以開啟 同意聲明對話方塊這個Intent只能使用一次。
    2. 如果 Intent 的活動結果是 Activity.RESULT_OK, 已授予同意聲明。再次致電 loadFriends() 退還好友 請參考閱讀清單,進一步瞭解 如何選擇 Kubeflow Pipelines SDK 或 TFX如果結果是 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.
Games.getPlayersClient(this, GoogleSignIn.getLastSignedInAccount(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 遊戲個人資料檢視畫面: 此檢視畫面可讓玩家傳送與接受好友邀請 目標動畫此檢視畫面不需要存取好友 請參考閱讀清單,進一步瞭解 如何選擇 Kubeflow Pipelines SDK 或 TFX另外,如果遊戲的玩家名稱概念與 Play 遊戲玩家 ID 不同,您可以將這些名稱傳遞至個人資料檢視畫面,方便納入好友邀請中,提供額外背景資訊。

如要顯示其他玩家的個人資料,請按照下列步驟操作:

  1. PlayersClient.getCompareProfileIntent()敬上 方法,此方法會傳回 Task 物件。
  2. 如果呼叫成功,Google Play 遊戲服務會傳回 將顯示畫面,讓使用者比較自己和其他項目 玩家個人資料。
  3. 使用上一個步驟中的 Intent 以啟動活動。
// Retrieve and launch an Intent to show a player profile within the game.
Games.getPlayersClient(this, GoogleSignIn.getLastSignedInAccount(this))
    .getCompareProfileIntent(otherPlayerId)
    .addOnSuccessListener(new OnSuccessListener<Intent>() {
        @Override
        public void onSuccess(Intent  intent) {
          startActivityForResult(intent, RC_SHOW_PROFILE);
          // ...
        }});

如果遊戲有特定的玩家名稱,可將這些 API 新增至 API 呼叫。 這會讓 Play 遊戲可以為傳送朋友的玩家設定暱稱 在您的遊戲中傳送「<game-specific-name>」的邀請來自 <your-game-name>」(Play 遊戲會自動在「 &lt;your-game-name&gt;&quot;):

// 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.
Games.PlayersClient(this, GoogleSignIn.getLastSignedInAccount(this))
    .getCompareProfileIntentWithAlternativeNameHints(otherPlayerId, otherPlayerInGameName, currentPlayerInGameName)
    .addOnSuccessListener(new OnSuccessListener<Intent>() {
        @Override
        public void onSuccess(Intent  intent) {
          startActivityForResult(intent, RC_SHOW_PROFILE);
          // ...
        }});