इस गाइड में, Android Studio प्रोजेक्ट में Friends API इस्तेमाल करने का तरीका बताया गया है.
दोस्तों की सूची लोड करना
मौजूदा उपयोगकर्ता के दोस्तों की सूची को गेम में वापस पाया और दिखाया जा सकता है. उपयोगकर्ता के तौर पर, यह कंट्रोल किया जा सकता है कि किन गेम के पास दोस्तों की सूची का ऐक्सेस हो. दोस्तों की सूची वापस पाने के लिए, आपको उस स्थिति को मैनेज करना होगा जहां अनुमति ज़रूरी है. इन सभी को एपीआई में शामिल किया गया है, ताकि ऐक्सेस का अनुरोध करना और इसके बाद दोस्तों की सूची का इस्तेमाल करना आसान हो. दोस्तों की सूची लोड करने के लिए, यह तरीका अपनाएं:
PlayersClient.loadFriends()
तरीके को कॉल करें. यह एक एसिंक्रोनस कॉल है, जोTask
ऑब्जेक्ट लौटाता है.- अगर कॉल पूरा होता है (उपयोगकर्ता ने पहले ही दोस्तों की सूची को ऐक्सेस करने की अनुमति दे दी है), तो Google Play Games Services, एनोटेट किया गया
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 Games प्रोफ़ाइल दिखाने का विकल्प होता है. इस व्यू में, खिलाड़ी को दोस्त बनने का न्योता भेजा जा सकता है और न्योते स्वीकार किए जा सकते हैं. इस व्यू के लिए, दोस्तों की सूची का ऐक्सेस ज़रूरी नहीं है. इसके अलावा, अगर आपके गेम में प्लेयर के नाम का कॉन्सेप्ट, Play Games के गेमर आईडी से अलग है, तो इन्हें प्रोफ़ाइल व्यू में पास किया जा सकता है. इससे इन्हें ज़्यादा जानकारी के लिए, दोस्त के न्योते में शामिल किया जा सकता है.
किसी दूसरे प्लेयर की प्रोफ़ाइल दिखाने के लिए, यह तरीका अपनाएं:
PlayersClient.getCompareProfileIntent()
तरीके को कॉल करें. यह एक एसिंक्रोनस कॉल है, जोTask
ऑब्जेक्ट लौटाता है.- अगर कॉल पूरा होता है, तो Google Play Games Services एक 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);
// ...
}});
अगर गेम में खिलाड़ियों के लिए कोई नाम है, तो उन्हें एपीआई कॉल में जोड़ा जा सकता है. इससे Play Games, उन खिलाड़ियों का निकनेम सेट कर पाता है जो आपके गेम से "<your-game-name> से <game-specific-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 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);
// ...
}});