با توصیه هایی که در چک لیست کیفیت توضیح داده شده است آشنا شوید.
مشتری دستاوردها دریافت کنید
برای شروع استفاده از API دستاوردها، بازی شما باید ابتدا یک شیء AchievementsClient را دریافت کند. می توانید این کار را با فراخوانی متد Games.getAchievementClient() و عبور از اکتیویتی انجام دهید.
قفل دستاوردها را باز کنید
برای باز کردن قفل یک دستاورد، متد AchievementsClient.unlock() را فراخوانی کرده و شناسه دستاورد را ارسال کنید.
قطعه کد زیر نشان می دهد که چگونه برنامه شما می تواند قفل دستاوردها را باز کند:
برای باز کردن قفل دستاورد، نیازی به نوشتن کد اضافی ندارید. خدمات بازیهای Google Play بهصورت خودکار قفل دستاورد را پس از رسیدن به تعداد مراحل لازم باز میکند.
یک تمرین خوب این است که شناسه های دستاورد را در فایل strings.xml تعریف کنید تا بازی شما بتواند دستاوردها را با شناسه منبع ارجاع دهد. هنگام برقراری تماس برای بهروزرسانی و بارگیری دستاوردها، مطمئن شوید که این بهترین روشها را نیز دنبال میکنید تا از تجاوز از سهمیه API خود جلوگیری کنید.
قطعه کد زیر نشان می دهد که چگونه برنامه شما می تواند رابط کاربری پیش فرض دستاورد را نمایش دهد. در قطعه، RC_ACHIEVEMENT_UI یک عدد صحیح دلخواه است که بازی از آن به عنوان کد درخواست استفاده می کند.
نمونهای از رابط کاربری پیشفرض دستاوردها در زیر نشان داده شده است.
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و OpenJDK علامتهای تجاری یا علامتهای تجاری ثبتشده Oracle و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-07-29 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","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-29 بهوقت ساعت هماهنگ جهانی."],[],[],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."]]