لبدء استخدام واجهة برمجة التطبيقات "إنجازات Google Play"، يجب أن تحصل لعبتك أولاً على عنصر
AchievementsClient. يمكنك إجراء ذلك من خلال استدعاء الأسلوب
Games.getAchievementClient()
وإرسال النشاط.
لست بحاجة إلى كتابة رمز إضافي لفتح الإنجاز، لأنّ "خدمات ألعاب Play" تفتح الإنجاز تلقائيًا عند بلوغ العدد المطلوب من الخطوات.
من الممارسات الجيدة تحديد أرقام تعريف الإنجازات في ملف strings.xml، لكي تتمكّن
لعبتك من الإشارة إلى الإنجازات حسب رقم تعريف المورد. عند إجراء طلبات لتعديل الإنجازات وتحميلها، احرص أيضًا على اتّباع أفضل الممارسات التالية لتجنُّب تجاوز حصة واجهة برمجة التطبيقات.
يوضّح مقتطف الرمز البرمجي التالي كيفية عرض تطبيقك لواجهة مستخدِم الإنجاز التلقائية. في المقتطف، RC_ACHIEVEMENT_UI هو عدد كلي
عشوائي تستخدمه اللعبة كرمز الطلب.
في ما يلي مثال على واجهة مستخدم الإنجازات التلقائية.
يخضع كل من المحتوى وعيّنات التعليمات البرمجية في هذه الصفحة للتراخيص الموضحّة في ترخيص استخدام المحتوى. إنّ Java وOpenJDK هما علامتان تجاريتان مسجَّلتان لشركة Oracle و/أو الشركات التابعة لها.
تاريخ التعديل الأخير: 2025-07-26 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","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 (حسب التوقيت العالمي المتفَّق عليه)"],[],[],null,["# Achievements for Android games\n\n| **Note:** This guide is for the Play Games Services v2 SDK. For information about the previous version of this SDK, see the [Play Games Services v1\n| documentation](/games/pgs/v1/android/achievements).\n\nThis guide shows you how to use the achievements APIs in an Android application\nto unlock and display achievements in your game. The APIs can be found\nin the [`com.google.android.gms.games`](https://developers.google.com//android/reference/com/google/android/gms/games/package-summary)\nand [`com.google.android.gms.games.achievements`](https://developers.google.com//android/reference/com/google/android/gms/games/achievement/package-summary)\npackages.\n\nBefore you begin\n----------------\n\nIf you haven't already done so, you might find it helpful to review the\n[achievements game concepts](/games/pgs/achievements).\n\nBefore you start to code using the achievements API:\n\n- Follow the instructions for installing and setting up your app to use\n Google Play Games Services in the\n [Set Up Google Play Services SDK](https://developers.google.com/android/guides/setup) guide.\n\n- Define the achievements that you want your game to unlock or display, by\n following the instructions in the [Google Play Console guide](/games/pgs/achievements#creating_an_achievement).\n\n- Download and review the achievements code samples in the\n [Android samples page](https://github.com/playgameservices/android-basic-samples).\n\n- Familiarize yourself with the recommendations described in\n [Quality Checklist](/games/pgs/quality).\n\nGet an achievements client\n--------------------------\n\nTo start using the achievements API, your game must first obtain an\n[`AchievementsClient`](https://developers.google.com/android/reference/com/google/android/gms/games/AchievementsClient)\nobject. You can do this by calling the\n[`Games.getAchievementClient()`](https://developers.google.com/android/reference/com/google/android/gms/games/AchievementsClient#public-abstract-taskintent-getachievementsintent)\nmethod and passing in the activity.\n| **Note:** The [`AchievementsClient`](https://developers.google.com/android/reference/com/google/android/gms/games/AchievementsClient) class makes use of the Google Play services [`Task`](https://developers.google.com/android/reference/com/google/android/gms/tasks/Task) class to return results asynchronously. To learn more about using tasks to manage threaded work, see the [Tasks API developer guide](https://developers.google.com/android/guides/tasks).\n\nUnlock achievements\n-------------------\n\nTo unlock an achievement, call the\n[`AchievementsClient.unlock()`](https://developers.google.com/android/reference/com/google/android/gms/games/AchievementsClient#unlock(java.lang.String))\nmethod and pass in the achievement ID.\n\nThe following code snippet shows how your app can unlock achievements: \n\n```scdoc\nPlayGames.getAchievementsClient(this).unlock(getString(R.string.my_achievement_id));\n```\n\nIf the achievement is of the *incremental* type (that is, several steps are\nrequired to unlock it), call [`AchievementsClient.increment()`](https://developers.google.com/android/reference/com/google/android/gms/games/AchievementsClient#increment(java.lang.String,%20int))\ninstead.\n\nThe following code snippet shows how your app can increment the player's\nachievement: \n\n```scdoc\nPlayGames.getAchievementsClient(this).increment(getString(R.string.my_achievement_id), 1);\n```\n\nYou don't need to write additional code to unlock the achievement; Google Play Games Services\nautomatically unlocks the achievement once it reaches its required number of\nsteps.\n\nA good practice is to define the achievement IDs in the `strings.xml` file, so\nyour game can reference the achievements by resource ID. When making calls to\nupdate and load achievements, make sure to also follow these\n[best practices](/games/pgs/quality) to avoid exceeding your API\nquota.\n\nDisplay achievements\n--------------------\n\nTo show a player's achievements, call\n[`AchievementsClient.getAchievementsIntent()`](https://developers.google.com/android/reference/com/google/android/gms/games/AchievementsClient#public-abstract-taskintent-getachievementsintent)\nto get an\n[`Intent`](http://developer.android.com/reference/android/content/Intent.html)\nto create the default achievements user interface. Your game can then bring up\nthe UI by calling\n[`startActivityForResult`](http://developer.android.com/reference/android/app/Activity#startActivityForResult(android.content.Intent,%20int)).\n\nThe following code snippet shows how your app can display the default\nachievement user interface. In the snippet, `RC_ACHIEVEMENT_UI` is an arbitrary\ninteger that the game uses as the request code. \n\n```transact-sql\nprivate static final int RC_ACHIEVEMENT_UI = 9003;\n\nprivate void showAchievements() {\n PlayGames.getAchievementsClient(this)\n .getAchievementsIntent()\n .addOnSuccessListener(new OnSuccessListener\u003cIntent\u003e() {\n @Override\n public void onSuccess(Intent intent) {\n startActivityForResult(intent, RC_ACHIEVEMENT_UI);\n }\n });\n}\n```\n\nAn example of the default achievements UI is shown below."]]