קל לארגן דפים בעזרת אוספים
אפשר לשמור ולסווג תוכן על סמך ההעדפות שלך.
כדי לגשת לפונקציונליות של Google Play Games Services, המשחק צריך לספק את החשבון של השחקן שנכנס. במסמך הזה מוסבר איך להטמיע חוויית כניסה חלקה במשחק.
ב-SDK של Play Games Services v2 יש כמה שיפורים שיעזרו לכם להגדיל את מספר המשתמשים שנכנסים לחשבון במשחק, וגם יעזרו לכם לפתח את המשחק בקלות רבה יותר:
שיפורים למשתמשים:
אחרי שבוחרים חשבון ברירת מחדל, המשתמשים מחוברים לחשבון בלי צורך לבצע פעולה כלשהי.
משתמשים כבר לא צריכים להוריד את אפליקציית Play Games כדי להיכנס באמצעות Play Games Services או ליצור חשבון חדש.
עכשיו המשתמשים יכולים לנהל את החשבונות שלהם ב-Play Games Services במספר משחקים מדף אחד.
שיפורים למפתחים:
קוד הלקוח כבר לא צריך לטפל בתהליך הכניסה או היציאה, כי הכניסה מופעלת באופן אוטומטי כשהמשחק מתחיל, וניהול החשבון מתבצע בהגדרות של מערכת ההפעלה.
שילוב לקוח חדש
בקטע הזה מוסבר איך לבצע שילוב לקוח חדש עם 'כניסה באמצעות חשבון Play Games Services' גרסה 2.
מוסיפים את יחסי התלות
מוסיפים את התלות ב-SDK של Play Games Services לקובץ build.gradle ברמת הבסיס של האפליקציה. אם משתמשים ב-Gradle, אפשר להוסיף או לעדכן את יחסי התלות באופן הבא:
מגדירים את ההפניה למשאב המחרוזת @string/game_services_project_id
באמצעות מזהה הפרויקט של שירותי המשחקים של המשחקים שלכם בתור הערך. מזהה הפרויקט של Games Services מופיע מתחת לשם המשחק בדף Configuration ב-Google Play Console.
בקובץ res/values/strings.xml, מוסיפים הפניה למשאב מחרוזת ומגדירים את מזהה הפרויקט בתור הערך. מזהה הפרויקט מופיע ב-Google Play Console מתחת לשם המשחק בדף Configuration. לדוגמה:
<!-- res/values/strings.xml -->
<resources>
<!-- Replace 0000000000 with your game’s project id. Example value shown above. --> <string translatable="false" name="game_services_project_id"> 0000000000 </string>
</resources>
אתחול ה-SDK
מפעילים את Play Games SDK ב-callback של onCreate(..) בכיתה Application.
כשהמשחק יופעל, תמיד תתבצע ניסיון כניסה של המשתמש. כדי לאמת את המשתמש, צריך לוודא שהמשתמש נכנס בהצלחה ואז לקבל את מזהה הנגן שלו.
כדי לאמת את ניסיון הכניסה, צריך להתקשר ל-GamesSignInClient.isAuthenticated() ולהשתמש ב-addOnCompleteListener כדי לאחזר את התוצאות. לדוגמה:
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().
}
});
אם המשתמש מחליט לא להיכנס לחשבון כשהמשחק מופעל, תוכלו להמשיך להציג לחצן עם סמל Play Games ולנסות להיכנס שוב לחשבון של המשתמש על ידי קריאה ל-GamesSignInClient.signIn() אם המשתמש ילחץ על הלחצן.
אחרי שתאמתו שהמשתמש נכנס לחשבון, תוכלו לאחזר את מזהה המשתמש כדי לזהות אותו. לדוגמה:
PlayGames.getPlayersClient(activity).getCurrentPlayer().addOnCompleteListener(mTask -> {
// Get PlayerID with mTask.getResult().getPlayerId()
}
);
דוגמאות התוכן והקוד שבדף הזה כפופות לרישיונות המפורטים בקטע רישיון לתוכן. Java ו-OpenJDK הם סימנים מסחריים או סימנים מסחריים רשומים של חברת Oracle ו/או של השותפים העצמאיים שלה.
עדכון אחרון: 2025-07-26 (שעון UTC).
[[["התוכן קל להבנה","easyToUnderstand","thumb-up"],["התוכן עזר לי לפתור בעיה","solvedMyProblem","thumb-up"],["סיבה אחרת","otherUp","thumb-up"]],[["חסרים לי מידע או פרטים","missingTheInformationINeed","thumb-down"],["התוכן מורכב מדי או עם יותר מדי שלבים","tooComplicatedTooManySteps","thumb-down"],["התוכן לא עדכני","outOfDate","thumb-down"],["בעיה בתרגום","translationIssue","thumb-down"],["בעיה בדוגמאות/בקוד","samplesCodeIssue","thumb-down"],["סיבה אחרת","otherDown","thumb-down"]],["עדכון אחרון: 2025-07-26 (שעון UTC)."],[],[],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."]]