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

ใช้ขั้นตอนในคู่มือนี้เพื่อติดตั้ง API ของเพื่อนในโค้ดเกม Java

โหลดเพื่อน

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

  1. เรียกใช้ PlayersClient.loadFriends() ซึ่งเป็นการเรียกแบบไม่พร้อมกันที่ส่งคืน ออบเจ็กต์ Task
  2. หากการโทรสำเร็จ (ผู้ใช้ให้สิทธิ์เพื่อนเข้าถึงแล้ว รายการ) บริการเกมของ Google Play จะแสดงคำอธิบายประกอบ PlayerBuffer ที่แสดงถึงเพื่อนของผู้ใช้
  3. หากโปรแกรมเล่นจำเป็นต้องให้สิทธิ์การเข้าถึงรายชื่อเพื่อน จะไม่สามารถโทรพร้อมกับ CANNOT TRANSLATE 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 ตั้งชื่อเล่นของผู้เล่นที่ส่งเพื่อนได้ คำเชิญจากภายในเกมไปยัง "<ชื่อเฉพาะเกม> จาก <ชื่อเกมของคุณ>" (Play Games จะเพิ่ม "จาก" โดยอัตโนมัติ &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.
PlayGames.PlayersClient(this)
    .getCompareProfileIntentWithAlternativeNameHints(otherPlayerId, otherPlayerInGameName, currentPlayerInGameName)
    .addOnSuccessListener(new OnSuccessListener<Intent>() {
        @Override
        public void onSuccess(Intent  intent) {
          startActivityForResult(intent, RC_SHOW_PROFILE);
          // ...
        }});