دمج واجهات برمجة تطبيقات Friends (Java)

استخدم الخطوات الواردة في هذا الدليل لتنفيذ واجهات Friends API في رمز لعبتك في Java.

تحميل الأصدقاء

يمكنك استرداد قائمة باللاعبين الأصدقاء وعرضها (في اللعبة). مع المستخدم الحالي. يمكن كمستخدم التحكم في الألعاب التي الوصول إلى قائمة الأصدقاء. عند استرداد قائمة الأصدقاء، يجب التعامل مع في الحالة التي يكون فيها الإذن مطلوبًا. يتم تغليف كل ذلك في واجهة برمجة التطبيقات جعل طلب الوصول واستخدام قائمة الأصدقاء بعد ذلك عملية المهمة. لتحميل قائمة الأصدقاء، اتبع الخطوات التالية:

  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.
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" من داخل لعبتك. تتيح طريقة العرض هذه للّاعبين إرسال دعوات الصداقة وقبولها. للاعب الذي يتم عرضه. لا تتطلب طريقة العرض هذه الوصول إلى الأصدقاء الحالية. بالإضافة إلى ذلك، إذا كان للعبة مفهومها الخاص عن أسماء اللاعبين بشكل منفصل من الأسماء التعريفية للّاعبين في "ألعاب Play"، يمكنك تمريرها إلى عرض الملف الشخصي بحيث يمكن تضمينها في دعوات أي صديق للحصول على سياق إضافي.

لعرض الملف الشخصي للاعب آخر، يُرجى اتّباع الخطوات التالية:

  1. عليك استدعاء PlayersClient.getCompareProfileIntent() وهي طلب غير متزامن يعرض الكائن Task.
  2. وفي حال نجاح الاتصال، تعرض "خدمات ألعاب Google Play" هدفًا سيعرض شاشة حيث يمكن للمستخدم مقارنة نفسه بأخرى للملف الشخصي للاعب.
  3. ويمكنك استخدام 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" تحديد أسماء اللاعبين الذين يرسلون أصدقاء. دعوات من داخل لعبتك إلى "<game-specific-name>" من <اسم لعبتك>" (يلحق تطبيق "ألعاب Play" بالعبارة "من" &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.
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);
          // ...
        }});