加载好友:您可以查看玩家是否已允许游戏访问好友列表信息。授予访问权限后,您可以获取与已登录玩家的好友相对应的 Player 对象列表。
启动另一个玩家的 Play 游戏玩家资料的视图:您可以打开此视图,其中将显示已登录玩家为其他玩家指定的名称。此视图还提供好友管理控件,并且不会使玩家退出游戏。
提供用户控件:这些控件可允许用户管理其 Play 游戏玩家资料是否向好友显示以及其好友列表是否向游戏显示。对于好友列表访问权限,用户可以选择自动为所有游戏授予访问权限,也可以选择单独为每个游戏授予访问权限。因此,加载好友列表时可能会产生一个回调,该回调将显示获取访问权限的请求。
导入 Play 游戏好友列表
您可以使用 Friends API 来获取玩家的 Play 游戏好友列表,并将其添加到游戏内的好友列表中。
新用户将拥有一个起始好友列表,现有用户则可以将其 Play 游戏好友导入任何游戏内好友列表。
因此,您的用户将能够与尽可能多的玩家一起玩游戏,或者进行对战。
将 Play 游戏好友添加到您的游戏中
如需将 Play 游戏好友添加到任何现有的游戏内好友列表中,请将 Play 游戏好友的 Play 游戏服务 ID 与您的内部数据库中的相应玩家信息相关联。请确保这些好友旁边会显示一个带有 Play 游戏图标的按钮。用户按下该按钮可以查看其他玩家的资料,以便确认好友的身份。
从后端服务器使用好友列表时,请使用 REST API 安全地进行加载,而不是传递 Android API 的结果。请确保使用 REST API 中的 players.get(me) 针对当前登录玩家返回的玩家 ID,因为这将与其他玩家看到的 ID 相一致。
如果未存储扩充的好友列表(但仅在查看时使用),则无需执行任何额外的操作。
授予 Play 游戏访问权限
如果您的游戏尚未获得 Play 游戏访问权限,一种有效的方式是在用户查看游戏内好友列表时通过弹出式窗口向用户征求同意。例如,您可以添加一个名为 Import Play Games friends 的按钮,当用户点按该按钮时,系统会向用户征求同意。(请务必在任何提及服务的按钮上使用 Play 游戏徽标。)
查看其他玩家的个人资料
您可以允许已登录玩家查看其他玩家的 Play 游戏玩家资料。这样一来,已登录玩家就可以查看为其他玩家指定的名称,以及他们是否已经是好友,从而为其他玩家添加其关系的相关背景信息。如果玩家还不是好友,则已登录玩家将在玩家资料视图中看到好友管理控件。在游戏内建立好友关系时,两位玩家的默认名称均为各自的游戏内名称(如果提供)。此外,还会显示发起了好友关系的游戏的名称。
社交排行榜
Friends API 也可用于排行榜。此功能可用于显示当前玩家在其 Play 游戏好友中的排名。请注意,这仅适用于已选择与您的应用共享此信息的用户,而且这些用户已经在 Play 游戏中建立了好友关系。为了支持此功能,游戏为用户提供了控件。此控件使用 collection 参数来选择排行榜的社交视图。如需了解详情,请参阅公开排行榜和社交排行榜部分。
如果您实现了社交排行榜收藏集,则在调用加载排行榜得分时可能会返回类似于 loadFriends() 中的“需要征求用户同意 (consent-required)”解析异常。如果您实现了 Play 游戏提供的默认界面(例如 getLeaderboardIntent()),则系统会自动为您执行相关处理。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[],[],null,["# Friends\n\nUse the Friends APIs to complement and enhance your existing in-game friends system\nand other social systems your game may access. This lets you enable players to:\n\n- Find their friends in your game.\n\n- Compare scores with their friends on leaderboards.\n\n- Combine the list of their Play Games friends with existing in-game lists of\n friends.\n\n- Identify another player with an in-game profile popup. This popup shows a\n nickname that the current player has given to their friend, so they know who\n they are playing with.\n\nBasics\n------\n\nThese APIs allow you to perform the following actions:\n\n- **Load friends**: You can check if the player has allowed the game to access Friends list information. If access is granted, you can get a list of Player objects corresponding to the friends of the signed-in player.\n- **Launch a view of another player's Play Games profile**: You can open up this view to show the name given to the other player by the signed-in player. This view also offers friendship management controls and won't take the player out of your game.\n- **Provide user controls**: The user has controls to manage how their Play Games profile is visible to friends and how their friends list is visible to games. For friends list access, the user can choose to automatically grant access for all games or they can choose to individually approve access for each game. Consequently, when loading the friends list, the result may be a callback to display a request for access.\n\nImport a Play Games friends list\n--------------------------------\n\nYou can use the Friends APIs to get a list of your players' Play Games\nfriends and add them to your in-game friends list.\n\nNew users will have a starting list of friends to play with, and existing users\ncan import their Play Games friends into any in-game lists of friends.\nAs a result, your users will have the largest possible set of players to play\nwith or compete against.\n| **Note:** If you store any relationships from Play Games, you must regularly check the friends list to ensure that the relationships, and consent to use them, are still valid.\n\n### Add Play Games friends to your game\n\nAdd Play Games friends to any existing in-game friends list by\nassociating their Play Games ID with corresponding player information in\nyour internal database. Make sure you have a button with the Play Games\nicon next to these friends, which shows the other player's profile when pressed,\nso your users can know who the friends are.\n\nWhen using the friends list from a backend server,\n[load it securely](/games/pgs/signin#secure-access)\nusing the REST API rather than passing the result of the Android API. Make sure\nto use the player ID returned by\n[`players.get(me)`](/games/services/web/api/rest/v1/players/get)\nin the REST API for the currently signed-in player, as this will be consistent\nwith the ID seen by other players.\n\nIf the augmented friends list is not stored (but just used at the time of\nviewing), then no additional work is needed.\n\n### Grant Play Games access\n\nIf your game doesn't already have Play Games access, a good time to\nprompt users for their consent is when they view your in-game friends list. For\nexample, you might add a button called **Import Play Games friends**, which\nprompts the user for consent when tapped. (Make sure to use the\nPlay Games logo on any button that mentions the service.)\n\nView another player's profile\n-----------------------------\n\nYou can allow your signed-in player to view another player's\nPlay Games profile. This allows the signed-in player to see the name they\nhave given the other player and whether or not they are already friends, giving\nthem added context about the relationship. If the players are not yet friends,\nthe signed-in player will see friendship management controls on the profile\nview. When friendships are created from within the game, the default names for\nthe two players are their in-game names (if provided). The name of the\ngame where the friendship was initiated is also shown.\n\nSocial leaderboards\n-------------------\n\nThe Friends APIs can also be used for leaderboards. Use this feature to show the\nranking of the current player among their Play Games friends. Note that\nthis only applies to users who have chosen to share this information with your\napplication, and if the users are friends in Play Games. To support this\nfeature, the game exposes a control to the user. This control uses the\n`collection` argument to select the social view of the leaderboard. To learn\nmore, see the section on\n[public and social leaderboards](/games/pgs/leaderboards#public_and_social_leaderboards).\n\nIf you implement the social leaderboards collection, the call to load the\nleaderboard scores may return a consent-required resolution exception similar to\nthat from\n[`loadFriends()`](https://developers.google.com/android/reference/com/google/android/gms/games/PlayersClient#loadFriends(int,%20boolean)).\nIf you implement the default Play Games-provided UI (for example,\n[`getLeaderboardIntent()`](https://developers.google.com/android/reference/com/google/android/gms/games/LeaderboardsClient.html#getLeaderboardIntent(java.lang.String))),\nthen this is handled for you automatically.\n\nNext steps\n----------\n\nBefore you start to use the Friends APIs, do the following:\n\n- Download and review a code sample:\n - [Java](https://github.com/playgameservices/android-basic-samples)\n - [Unity sample app](https://github.com/playgameservices/play-games-plugin-for-unity/tree/master/Samples/SmokeTest) using the [Unity plugin](https://github.com/playgameservices/play-games-plugin-for-unity)\n- Familiarize yourself with the recommendations described in the [Quality Checklist](/games/pgs/quality#friends).\n- Implement the [Friends APIs in a Java client](/games/pgs/android/friends)."]]