在弃用 Google 登录 API 后,我们将于 2026 年移除 games v1 SDK。2025 年 2 月之后,您将无法在 Google Play 上发布新集成了 games v1 SDK 的游戏。我们建议您改用 games v2 SDK。
虽然采用旧版游戏 v1 集成的现有游戏仍可在未来几年内正常运行,但我们建议您从 2025 年 6 月开始迁移到 v2。
本指南介绍了如何使用 Play 游戏服务 v1 SDK。如需了解最新的 SDK 版本,请参阅 v2 文档。
按照本指南中的步骤,在 Java 游戏代码中实现 Friends API。
加载好友
您可以(在游戏中)检索并显示与当前用户是好友关系的玩家列表。用户可以控制哪些游戏具有对好友列表的访问权限。检索好友列表时,您需要处理需要权限的情况。这一切都封装在 API 中,使得请求访问权限以及随后使用好友列表变得很简单直观。如需加载好友列表,请按以下步骤操作:
// 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(newOnSuccessListener<AnnotatedData<PlayerBuffer>>(){@OverridepublicvoidonSuccess(AnnotatedData<PlayerBuffer>data){PlayerBufferplayerBuffer=data.get();// ...}).addOnFailureListener(exception->{if(exceptioninstanceofFriendsResolutionRequiredException){PendingIntentpendingIntent=((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. */@OverridepublicvoidonActivityResult(intrequestCode,intresult,Intentdata){if(requestCode==SHOW_SHARING_FRIENDS_CONSENT){if(result==Activity.RESULT_OK){// We got consent from the user to access their friends. Retry loading the friendscallLoadFriends();}else{// User did not grant consent.}}}
查看其他玩家的个人资料
您可以在游戏中显示其他玩家的 Play 游戏玩家资料的视图。此视图可允许玩家向正在查看的玩家发送好友邀请,或接受对方发来的好友邀请。此视图不需要访问好友列表。此外,如果您的游戏使用与 Play 游戏玩家 ID 不同的玩家名称概念,您可以将这些名称传递给玩家资料视图,以支持将这些名称包含在任何好友邀请中以提供更多背景信息。
如果调用成功,Google Play 游戏服务会返回一个 intent,它会显示一个屏幕,供用户将自己与其他玩家的资料进行比较。
使用上一步中的 Intent 启动 activity。
// Retrieve and launch an Intent to show a player profile within the game.Games.getPlayersClient(this,GoogleSignIn.getLastSignedInAccount(this)).getCompareProfileIntent(otherPlayerId).addOnSuccessListener(newOnSuccessListener<Intent>(){@OverridepublicvoidonSuccess(Intentintent){startActivityForResult(intent,RC_SHOW_PROFILE);// ...}});
如果游戏为玩家使用了自己的名称,则可以将这些名称添加到 API 调用中。这样一来,Play 游戏就可以将在您的游戏中发送好友邀请的玩家的别名设置为“<game-specific-name> from <your-game-name>”。Play 游戏会自动附加“from <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 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(newOnSuccessListener<Intent>(){@OverridepublicvoidonSuccess(Intentintent){startActivityForResult(intent,RC_SHOW_PROFILE);// ...}});
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Integrate the Friends APIs (Java)\n\nFollowing the deprecation of the\n[Google Sign-In](https://android-developers.googleblog.com/2024/09/streamlining-android-authentication-credential-manager-replaces-legacy-apis.html)\nAPI, we are removing the games v1 SDK in 2026. After February 2025, you will be unable to publish\ntitles that are newly integrated with games v1 SDK, on Google Play. We recommend that you use the\ngames v2 SDK instead. \n\nWhile existing titles with the previous games v1 integrations continue to function for a\ncouple of years, you are encouraged to\n[migrate to v2](/games/pgs/android/migrate-to-v2)\nstarting June 2025. \n\nThis guide is for using the Play Games Services v1 SDK. For information\non the latest SDK version, see the\n[v2 documentation](/games/pgs/android/friends).\n\nUse the steps in this guide to implement the Friends APIs in your Java game code.\n\nLoad friends\n------------\n\nYou can retrieve and display (in the game) a list of players who are friends\nwith the current user. As a user, it is possible to control which games have\naccess to the friends list. When you retrieve the friends list, you must handle\nthe case where permission is required. This is all encapsulated in the API to\nmake requesting access and subsequently using the friends list a straightforward\ntask. To load the friends list, follow these steps:\n\n1. Call the [`PlayersClient.loadFriends()`](https://developers.google.com/android/reference/com/google/android/gms/games/PlayersClient#loadFriends(int,%20boolean)) method, which is an asynchronous call returning a [`Task`](https://developers.google.com/android/reference/com/google/android/gms/tasks/Task) object.\n2. If the call is successful (the user already granted access to the friends list), Google Play Games Services returns an annotated [`PlayerBuffer`](https://developers.google.com/android/reference/com/google/android/gms/games/PlayerBuffer) that represents the user's friends.\n3. If the player needs to grant access to the friends list, the call fails with\n a\n [`FriendsResolutionRequiredException`](https://developers.google.com/android/reference/com/google/android/gms/games/FriendsResolutionRequiredException).\n No dialogs are shown yet.\n\n 1. This exception contains an `Intent` that triggers a dialog to ask the player for consent. You can launch this `Intent` immediately to open a consent dialog. You can only use this `Intent` once.\n 2. If the result of the `Intent`'s activity is `Activity.RESULT_OK`, then\n consent was granted. Call `loadFriends()` again to return the friends\n list. If the result is `Activity.RESULT_CANCELLED`, the user did\n not consent and `loadFriends()` will continue to return\n `FriendsResolutionRequiredException`.\n\n | **Note:** If you call `loadFriends()` from your server and determine that you need to request consent, you need to call `loadFriends()` again from your game client. This second call won't succeed, but it will provide you the `Intent` required to show the consent dialog.\n\nThe following code shows how to implement loading the friends list: \n\n // Attempt loading friends.\n // Register a success listener to handle the successfully loaded friends list.\n // Register a failure listener to handle asking for permission to access the list.\n Games.getPlayersClient(this, GoogleSignIn.getLastSignedInAccount(this))\n .loadFriends(PAGE_SIZE, /* forceReload= */ false)\n .addOnSuccessListener(\n new OnSuccessListener\u003cAnnotatedData\u003cPlayerBuffer\u003e\u003e() {\n @Override\n public void onSuccess(AnnotatedData\u003cPlayerBuffer\u003e data) {\n PlayerBuffer playerBuffer = data.get();\n // ...\n })\n\n .addOnFailureListener(\n exception -\u003e {\n if (exception instanceof FriendsResolutionRequiredException) {\n PendingIntent pendingIntent =\n ((FriendsResolutionRequiredException) task.getException())\n .getResolution();\n parentActivity.startIntentSenderForResult(\n pendingIntent.getIntentSender(),\n /* requestCode */ SHOW_SHARING_FRIENDS_CONSENT,\n /* fillInIntent */ null,\n /* flagsMask */ 0,\n /* flagsValues */ 0,\n /* extraFlags */ 0,\n /* options */ null);\n }\n });\n return;\n }\n\nThe following code shows how to handle the result from the request for consent: \n\n /** Handle the activity result from the request for consent. */\n @Override\n public void onActivityResult(int requestCode, int result, Intent data) {\n if (requestCode == SHOW_SHARING_FRIENDS_CONSENT) {\n if (result == Activity.RESULT_OK) {\n // We got consent from the user to access their friends. Retry loading the friends\n callLoadFriends();\n } else {\n // User did not grant consent.\n }\n }\n }\n\nView another player's profile\n-----------------------------\n\nYou can display a view of another player's Play Games profile from\nwithin your game. This view allows players to send and accept friend invitations\nfor the player being viewed. This view does not require access to the friends\nlist. Additionally, if your game has its own concept of player names separate\nfrom Play Games Gamer IDs, you can pass these along to the profile view\nso that they can be included in any friend invitations for additional context.\n\nTo show another player's profile, follow these steps:\n\n1. Call the [`PlayersClient.getCompareProfileIntent()`](https://developers.google.com/android/reference/com/google/android/gms/games/PlayersClient) method, which is an asynchronous call returning a [`Task`](https://developers.google.com/android/reference/com/google/android/gms/tasks/Task) object.\n2. If the call is successful, Google Play Games Services returns an Intent that will display a screen where the user can compare themselves against another player's profile.\n3. Use the `Intent` from the previous step to start an activity.\n\n // Retrieve and launch an Intent to show a player profile within the game.\n Games.getPlayersClient(this, GoogleSignIn.getLastSignedInAccount(this))\n .getCompareProfileIntent(otherPlayerId)\n .addOnSuccessListener(new OnSuccessListener\u003cIntent\u003e() {\n @Override\n public void onSuccess(Intent intent) {\n startActivityForResult(intent, RC_SHOW_PROFILE);\n // ...\n }});\n\nIf the game has its own name for players, these can be added to the API call.\nThis enables Play Games to set the nickname of players who send friend\ninvitations from within your game to \"\\\u003cgame-specific-name\\\u003e from\n\\\u003cyour-game-name\\\u003e\" (Play Games automatically appends \"from\n\\\u003cyour-game-name\\\u003e\"): \n\n // Show a player profile within the game, with additional hints containing the\n // game-specific names for both players.\n // - otherPlayerId is the Play Games playerId of the player to view.\n // - otherPlayerInGameName is the game-specific name of the player being viewed.\n // - currentPlayerInGameName is the game-specific name of the player who is signed\n // in. Hence if the player sends an invitation to the profile they are viewing,\n // their game-specific name can be included.\n Games.PlayersClient(this, GoogleSignIn.getLastSignedInAccount(this))\n .getCompareProfileIntentWithAlternativeNameHints(otherPlayerId, otherPlayerInGameName, currentPlayerInGameName)\n .addOnSuccessListener(new OnSuccessListener\u003cIntent\u003e() {\n @Override\n public void onSuccess(Intent intent) {\n startActivityForResult(intent, RC_SHOW_PROFILE);\n // ...\n }});"]]