คู่มือนี้อธิบายวิธีใช้ API ของเพื่อนในโปรเจ็กต์ Android Studio
โหลดเพื่อน
คุณสามารถดึงและแสดง (ในเกม) รายชื่อผู้เล่นที่เป็นเพื่อนกับผู้ใช้ปัจจุบันได้ ในฐานะผู้ใช้ คุณสามารถควบคุมเกมที่มีสิทธิ์เข้าถึงรายชื่อเพื่อนได้ เมื่อเรียกข้อมูลรายชื่อเพื่อน คุณต้องจัดการกรณีที่ต้องได้รับสิทธิ์ ซึ่งทั้งหมดนี้จะรวมอยู่ใน API เพื่อให้การขอสิทธิ์เข้าถึงและใช้รายชื่อเพื่อนเป็นงานที่ตรงไปตรงมา หากต้องการโหลดรายชื่อเพื่อน ให้ทำตามขั้นตอนต่อไปนี้
- เรียกใช้เมธอด
PlayersClient.loadFriends()
ซึ่งเป็นการเรียกแบบไม่พร้อมกันที่แสดงผลออบเจ็กต์Task
- หากการเรียกใช้สำเร็จ (ผู้ใช้ให้สิทธิ์เข้าถึงรายชื่อเพื่อนแล้ว) บริการเกมของ Google Play จะแสดงผล
PlayerBuffer
ที่มีคำอธิบายประกอบซึ่งแสดงถึงเพื่อนของผู้ใช้ หากผู้เล่นต้องให้สิทธิ์เข้าถึงรายชื่อเพื่อน การเรียกจะล้มเหลวพร้อมกับ a
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 Games ของผู้เล่นคนอื่นจากภายในเกมได้ มุมมองนี้ช่วยให้ผู้เล่นส่งและยอมรับคำเชิญเป็นเพื่อน สำหรับผู้เล่นที่กำลังดูอยู่ได้ มุมมองนี้ไม่จำเป็นต้องมีสิทธิ์เข้าถึงรายชื่อเพื่อน นอกจากนี้ หากเกมมีแนวคิดชื่อผู้เล่นของตัวเองแยกจากรหัสเกมเมอร์ Play Games คุณสามารถส่งชื่อเหล่านี้ไปยังมุมมองโปรไฟล์เพื่อให้ชื่อรวมอยู่ในคำเชิญเป็นเพื่อนเพื่อบริบทเพิ่มเติมได้
หากต้องการแสดงโปรไฟล์ของผู้เล่นคนอื่น ให้ทำตามขั้นตอนต่อไปนี้
- เรียกใช้เมธอด
PlayersClient.getCompareProfileIntent()
ซึ่งเป็นการเรียกแบบไม่พร้อมกันที่แสดงผลออบเจ็กต์Task
- หากการเรียกสำเร็จ บริการเกมของ Google Play จะแสดง Intent ที่ จะแสดงหน้าจอที่ผู้ใช้สามารถเปรียบเทียบโปรไฟล์ของตนเองกับโปรไฟล์ของผู้เล่นคนอื่นได้
- ใช้
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 จะต่อท้าย "จาก <ชื่อเกมของคุณ>" โดยอัตโนมัติ)
// 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);
// ...
}});