با مجموعهها، منظم بمانید ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
برای دسترسی به API های ارائه شده توسط سرویس های Google Play در برنامه Android خود، باید از اشیاء مشتری API استفاده کنید. این اشیاء اتصال به سرویسهای Google Play را کنترل میکنند، درخواستها را در صف قرار میدهند و زمانی که اتصال در دسترس است، آنها را به ترتیب اجرا میکنند. میتوانید در صورت نیاز کلاینتهای API جدیدی ایجاد کنید، زیرا ساخت آنها ارزان است.
برای دسترسی به سرویسی که نیازی به مجوز ندارد، یک نمونه از شی مشتری سرویس ایجاد کنید که یک Context یا یک Object Activity را ارسال کنید. در صورت لزوم، از کاربران خواسته میشود تا خدمات Google Play را قبل از اجرای هر گونه تماس API ارتقا دهند.
قطعه کد زیر نحوه دریافت آخرین مکان شناخته شده دستگاه را با استفاده از Fused Location Provider نشان می دهد:
کاتلین
// Code required for requesting location permissions omitted for brevity.valclient=LocationServices.getFusedLocationProviderClient(this)// Get the last known location. In some rare situations, this can be null.client.lastLocation.addOnSuccessListener{location:Location? -> location?.let{// Logic to handle location object.}}
جاوا
// Code required for requesting location permissions omitted for brevity.FusedLocationProviderClientclient=LocationServices.getFusedLocationProviderClient(this);// Get the last known location. In some rare situations, this can be null.client.getLastLocation().addOnSuccessListener(this,location->{if(location!=null){// Logic to handle location object.}});
قبل از فعال کردن یک ویژگی که به یک API خدمات Google Play بستگی دارد، با فراخوانی checkApiAvailability() بررسی کنید که آیا API در دستگاه موجود است یا خیر.
قطعه کد زیر نشان می دهد که چگونه می توان بررسی کرد که آیا ارائه دهنده مکان ترکیب شده در دسترس است:
تاریخ آخرین بهروزرسانی 2025-08-15 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","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-08-15 بهوقت ساعت هماهنگ جهانی."],[[["\u003cp\u003eTo use Google Play services APIs, create API client objects which manage the connection and queue requests when offline.\u003c/p\u003e\n"],["\u003cp\u003eAccess services without authorization by obtaining the client object using the current \u003ccode\u003eContext\u003c/code\u003e or \u003ccode\u003eActivity\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eAccess services requiring authorization by signing in the user, requesting necessary permissions, and then getting the client object.\u003c/p\u003e\n"],["\u003cp\u003eCheck for API availability using \u003ccode\u003echeckApiAvailability()\u003c/code\u003e if you need to disable features when an API is unavailable, although API calls will fail gracefully if the API isn't present.\u003c/p\u003e\n"]]],["To use Google Play services APIs, create an API client object, which manages the connection and queues requests. For services not requiring authorization, obtain the client instance using `Context` or `Activity`; for example, the Fused Location Provider client. For services requiring authorization, sign the user in, request necessary permissions, and then get the client using `GoogleSignInAccount`. For example, the Fit API client to get the daily steps. Before making API calls check with `checkApiAvailability()`.\n"],null,["To access\n[APIs powered by Google Play services](/android/guides/setup#list-dependencies)\nin your Android app, you need to use API client objects. These objects handle\nthe connection to Google Play services, queueing requests and executing them in\norder when a connection is available. You can create new API clients as needed,\nas they are inexpensive to construct.\n\nGet started\n\nBefore you begin, make sure to\n[set up Google Play services](/android/guides/setup) in your app project.\n| **Warning:** APIs using [`GoogleApiClient`](/android/reference/com/google/android/gms/common/api/GoogleApiClient) are deprecated. If you are still using `GoogleApiClient`, migrate to the corresponding `GoogleApi`-based APIs by updating to the latest available SDK versions.\n\nTo access a service that doesn't require authorization, create an instance of\nthe service's client object, passing either a\n[`Context`](https://developer.android.com/reference/android/content/Context) or\nan [`Activity`](https://developer.android.com/reference/android/app/Activity)\nobject. If necessary, users are prompted to upgrade Google Play services before\nany API calls are executed.\n\nThe following code snippet shows how to get the device's last known location\nusing the Fused Location Provider: \n\nKotlin \n\n```kotlin\n// Code required for https://developer.android.com/training/location/permissions omitted for brevity.\nval client = LocationServices.getFusedLocationProviderClient(this)\n\n// Get the last known location. In some rare situations, this can be null.\nclient.lastLocation.addOnSuccessListener { location : Location? -\u003e\n location?.let {\n // Logic to handle location object.\n }\n}\n```\n\nJava \n\n```java\n// Code required for https://developer.android.com/training/location/permissions omitted for brevity.\nFusedLocationProviderClient client =\n LocationServices.getFusedLocationProviderClient(this);\n\n// Get the last known location. In some rare situations, this can be null.\nclient.getLastLocation()\n .addOnSuccessListener(this, location -\u003e {\n if (location != null) {\n // Logic to handle location object.\n }\n });\n```\n\nTo access APIs that require user authorization, follow the guide to\n[authorize access to Google user data](https://developer.android.com/identity/authorization).\nIf you are using an API that requires a `GoogleSignInAccount` object, use the\n[`AuthorizationResult#toGoogleSignInAccount()`](/android/reference/com/google/android/gms/auth/api/identity/AuthorizationResult#toGoogleSignInAccount())\nmethod.\n\nCheck for API availability\n\nBefore enabling a feature that depends on a Google Play services API, check if\nthe API is available on the device by calling\n[`checkApiAvailability()`](/android/reference/com/google/android/gms/common/GoogleApiAvailability#checkApiAvailability(com.google.android.gms.common.api.HasApiKey%3C?%3E,%20com.google.android.gms.common.api.HasApiKey%3C?%3E...)).\n| **Note:** This check is only needed if you need to disable a feature when an API is unavailable. You can also call API methods without this check, as they will fail if the API is unavailable.\n\nThe following code snippet shows how to check if the fused location provider is\navailable: \n\nKotlin \n\n```kotlin\nfun getLastLocationIfApiAvailable(context: Context?): Task\u003cLocation\u003e? {\n val client = getFusedLocationProviderClient(context)\n return GoogleApiAvailability.getInstance()\n .checkApiAvailability(client)\n .onSuccessTask { _ -\u003e client.lastLocation }\n .addOnFailureListener { _ -\u003e Log.d(TAG, \"Location unavailable.\")}\n}\n```\n\nJava \n\n```java\npublic Task\u003cLocation\u003e getLastLocationIfApiAvailable(Context context) {\n FusedLocationProviderClient client =\n getFusedLocationProviderClient(context);\n return GoogleApiAvailability.getInstance()\n .checkApiAvailability(client)\n .onSuccessTask(unused -\u003e client.getLastLocation())\n .addOnFailureListener(e -\u003e Log.d(TAG, \"Location unavailable.\"));\n}\n```"]]