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