ผสานรวม Friends API (Java)

ใช้ขั้นตอนในคําแนะนํานี้เพื่อใช้ Friends API ในโค้ดเกม Java

โหลดเพื่อน

คุณเรียกและแสดง (ในเกม) รายชื่อผู้เล่นที่เป็นเพื่อนกับผู้ใช้ปัจจุบันได้ ในฐานะผู้ใช้ คุณสามารถควบคุมเกมที่ เข้าถึงรายชื่อเพื่อนได้ เมื่อดึงข้อมูลรายชื่อเพื่อน คุณต้องจัดการกรณีที่จําเป็นต้องใช้สิทธิ์ ขั้นตอนนี้รวมอยู่ใน API ทั้งหมดเพื่อให้ขอสิทธิ์เข้าถึง แล้วใช้รายชื่อเพื่อนเป็นงานที่เรียบง่ายในภายหลัง หากต้องการโหลดรายชื่อเพื่อน ให้ทําตามขั้นตอนต่อไปนี้

  1. เรียกเมธอด PlayersClient.loadFriends() ซึ่งเป็นการเรียกแบบไม่พร้อมกันซึ่งจะแสดงออบเจ็กต์ Task
  2. หากการโทรสําเร็จ (ผู้ใช้มีสิทธิ์เข้าถึงรายชื่อเพื่อนแล้ว) บริการเกมของ Google Play จะแสดงคําอธิบายประกอบ PlayerBuffer ที่แสดงถึงเพื่อนของผู้ใช้
  3. หากโปรแกรมเล่นต้องให้สิทธิ์เข้าถึงรายชื่อเพื่อน การโทรจะไม่สําเร็จด้วย FriendsResolutionRequiredException ยังไม่มีกล่องโต้ตอบปรากฏขึ้น

    1. ข้อยกเว้นนี้จะมี Intent ที่ทริกเกอร์กล่องโต้ตอบเพื่อขอความยินยอมสําหรับโปรแกรมเล่น คุณเปิด Intent นี้ได้ทันทีเพื่อเปิดกล่องโต้ตอบความยินยอม คุณใช้ Intent นี้ได้เพียงครั้งเดียว
    2. หากผลลัพธ์ของกิจกรรม 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 Games ของผู้เล่นคนอื่นจากภายในเกมของคุณได้ มุมมองนี้ช่วยให้ผู้เล่นส่งและยอมรับคําเชิญให้เพื่อนสําหรับผู้เล่นที่กําลังดูอยู่ได้ มุมมองนี้ไม่จําเป็นต้องมีการเข้าถึงรายชื่อเพื่อน นอกจากนี้ หากเกมมีแนวคิดของชื่อผู้เล่นแยกต่างหากจากรหัสเกมเมอร์ของ Play Games คุณสามารถส่งตัวระบุเหล่านี้ตามมุมมองโปรไฟล์เพื่อให้รวมชื่อไว้ในคําเชิญเพื่อนสําหรับบริบทเพิ่มเติมได้

หากต้องการแสดงโปรไฟล์ของผู้เล่นคนอื่น ให้ทําตามขั้นตอนต่อไปนี้

  1. เรียกเมธอด PlayersClient.getCompareProfileIntent() ซึ่งเป็นการเรียกแบบไม่พร้อมกันซึ่งจะแสดงออบเจ็กต์ Task
  2. หากการโทรสําเร็จ บริการเกมของ Google Play จะแสดง Intent ที่จะแสดงหน้าจอที่ผู้ใช้เปรียบเทียบตัวเองกับโปรไฟล์ของผู้เล่นคนอื่นได้
  3. ใช้ 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 Games กําหนดชื่อเล่นของผู้เล่นที่ส่งคําเชิญเพื่อนจากในเกมเป็น "<game-specific-name> จาก <your-game-name>" (Play Games จะต่อท้าย "จาก <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);
          // ...
        }});