GamesSignInClient gamesSignInClient = PlayGames.getGamesSignInClient(getActivity());
gamesSignInClient.isAuthenticated().addOnCompleteListener(isAuthenticatedTask -> {
boolean isAuthenticated =
(isAuthenticatedTask.isSuccessful() &&
isAuthenticatedTask.getResult().isAuthenticated());
if (isAuthenticated) {
// Continue with Play Games Services
} else {
// Disable your integration with Play Games Services or show a
// login button to ask players to sign-in. Clicking it should
// call GamesSignInClient.signIn().
}
});
[null,null,["最后更新时间 (UTC):2025-07-26。"],[],[],null,["# Sign-in for Android games\n\n| **Note:** This guide is for the Play Games Services v2 SDK. For information on the previous version of this SDK, see the [Play Games Services v1\n| documentation](/games/pgs/v1/android/signin).\n\nIn order to access Google Play Games Services functionality, your game needs to provide the\nsigned-in player's account. This documentation describes how to\nimplement a seamless sign-in experience in your game.\n\nThe Play Games Services v2 SDK brings a number of improvements that increase the\nnumber of users signed into your game, and make development easier:\n\n- Improvements for users:\n - After selecting a default account, users are logged-in without needing to interact with a prompt.\n - Users no longer need to download the Play Games App to sign-in with Play Games Services or create a new account.\n - Users can now manage their Play Games Services accounts for multiple games from a single page.\n- Improvements for developers:\n - Client code no longer needs to handle the sign-in or sign-out flow, as login is automatically triggered when the game starts, and account management is handled in the OS settings.\n\n| **Note:** You must follow additional steps if you want to enable recall functionality. For more information, see [Integrate the PGS Recall API within\n| your game](/games/pgs/recall/recall-setup).\n\nNew client integration\n----------------------\n\nThis section shows how to do a new client integration with Play Games Services\nSign In v2.\n| **Note:** If you already have a client integration with v1, follow the instructions in [Migrate from v1 to v2](/games/pgs/android/migrate-to-v2) to migrate your integration.\n\n### Add the dependency\n\nAdd the Play Game Services SDK dependency to your app's root-level\n`build.gradle` file. If you are using Gradle, you can add or update the\ndependency as follows: \n\n dependencies {\n implementation \"com.google.android.gms:play-services-games-v2:+\"\n }\n\n### Define the project ID\n\nTo add the Play Games Services SDK project ID to your app, complete the\nfollowing steps:\n\n1. In your app's `AndroidManifest.xml` file, add the following `\u003cmeta-data\u003e`\n element and attributes to the `\u003capplication\u003e` element:\n\n \u003cmanifest\u003e\n \u003capplication\u003e\n \u003cmeta-data android:name=\"com.google.android.gms.games.APP_ID\"\n android:value=\"@string/game_services_project_id\"/\u003e\n \u003c/application\u003e\n \u003c/manifest\u003e\n\n Define the String resource reference `@string/game_services_project_id`\n using your games' Game services project id as the value. Your Games services\n project id can be found under your game name in the *Configuration* page on\n the Google Play Console.\n2. In your `res/values/strings.xml` file, add a string resource reference\n and set your project ID as the value. In Google Play Console, you can find your\n project ID under your game name in the **Configuration** page. For example:\n\n \u003c!-- res/values/strings.xml --\u003e\n \u003cresources\u003e\n \u003c!-- Replace 0000000000 with your game's project id. Example value shown above. --\u003e\n \u003cstring translatable=\"false\" name=\"game_services_project_id\"\u003e 0000000000 \u003c/string\u003e\n \u003c/resources\u003e\n\n### Initialize the SDK\n\nInitialize Play Games SDK in the `onCreate(..)` callback of your `Application`\nclass. \n\n import com.google.android.gms.games.PlayGamesSdk;\n\n ...\n\n @Override\n public void onCreate(){\n super.onCreate();\n PlayGamesSdk.initialize(this);\n }\n\n### Get the sign-in result\n\nWhen your game launches, it will always attempt to sign in the user. To\nauthenticate the user, you must verify that the user successfully signed in,\nand then get their Player ID.\n\nTo verify the sign in attempt, call `GamesSignInClient.isAuthenticated()` and\nuse `addOnCompleteListener` to retrieve the results. For example: \n\n GamesSignInClient gamesSignInClient = PlayGames.getGamesSignInClient(getActivity());\n\n gamesSignInClient.isAuthenticated().addOnCompleteListener(isAuthenticatedTask -\u003e {\n boolean isAuthenticated =\n (isAuthenticatedTask.isSuccessful() &&\n isAuthenticatedTask.getResult().isAuthenticated());\n\n if (isAuthenticated) {\n // Continue with Play Games Services\n } else {\n // Disable your integration with Play Games Services or show a\n // login button to ask players to sign-in. Clicking it should\n // call GamesSignInClient.signIn().\n }\n });\n\nIf the user chooses not to sign in when the game launches, you may optionally\nchoose to continue showing a button with the Play Games icon, and attempt to\nsign in the user again by calling `GamesSignInClient.signIn()` if the user\npresses the button.\n\nAfter verifying that the user is signed in, you can retrieve the Player ID to\nidentify the user. For example: \n\n PlayGames.getPlayersClient(activity).getCurrentPlayer().addOnCompleteListener(mTask -\u003e {\n // Get PlayerID with mTask.getResult().getPlayerId()\n }\n );\n\n| **Note:** You should not store the player ID returned from the Android SDK in the game's backend, as it's possible for an untrusted device to tamper with it. Instead, you should [enable server-side API access](/games/pgs/android/server-access) and retrieve the player ID or other data with a server-side call directly from the game's backend."]]