<br />

To access
[APIs powered by Google Play services](https://developers.google.com/android/guides/setup.md.txt#list-dependencies)
in your Android app, you need to use API client objects. These objects handle
the connection to Google Play services, queueing requests and executing them in
order when a connection is available. You can create new API clients as needed,
as they are inexpensive to construct.

## Get started

Before you begin, make sure to
[set up Google Play services](https://developers.google.com/android/guides/setup.md.txt) in your app project.

> [!WARNING]
> **Warning:** APIs using [`GoogleApiClient`](https://developers.google.com/android/reference/com/google/android/gms/common/api/GoogleApiClient.md.txt) are deprecated. If you are still using `GoogleApiClient`, migrate to the corresponding `GoogleApi`-based APIs by updating to the latest available SDK versions.

To access a service that doesn't require authorization, create an instance of
the service's client object, passing either a
[`Context`](https://developer.android.com/reference/android/content/Context) or
an [`Activity`](https://developer.android.com/reference/android/app/Activity)
object. If necessary, users are prompted to upgrade Google Play services before
any API calls are executed.

The following code snippet shows how to get the device's last known location
using the Fused Location Provider:

### Kotlin

```kotlin
// Code required for requesting location permissions omitted for brevity.
val client = 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.
    }
}
```

### Java

```java
// Code required for requesting location permissions omitted for brevity.
FusedLocationProviderClient client =
        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.
            }
        });
```

To access APIs that require user authorization, follow the guide to
[authorize access to Google user data](https://developer.android.com/identity/authorization).
If you are using an API that requires a `GoogleSignInAccount` object, use the
[`AuthorizationResult#toGoogleSignInAccount()`](https://developers.google.com/android/reference/com/google/android/gms/auth/api/identity/AuthorizationResult.md.txt#toGoogleSignInAccount())
method.

## Check for API availability

Before enabling a feature that depends on a Google Play services API, check if
the API is available on the device by calling
[`checkApiAvailability()`](https://developers.google.com/android/reference/com/google/android/gms/common/GoogleApiAvailability.md.txt#checkApiAvailability(com.google.android.gms.common.api.HasApiKey%3C?%3E,%20com.google.android.gms.common.api.HasApiKey%3C?%3E...)).

> [!NOTE]
> **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.

The following code snippet shows how to check if the fused location provider is
available:

### Kotlin

```kotlin
fun getLastLocationIfApiAvailable(context: Context?): Task<Location>? {
    val client = getFusedLocationProviderClient(context)
    return GoogleApiAvailability.getInstance()
        .checkApiAvailability(client)
        .onSuccessTask { _ -> client.lastLocation }
        .addOnFailureListener { _ -> Log.d(TAG, "Location unavailable.")}
}
```

### Java

```java
public Task<Location> getLastLocationIfApiAvailable(Context context) {
    FusedLocationProviderClient client =
            getFusedLocationProviderClient(context);
    return GoogleApiAvailability.getInstance()
            .checkApiAvailability(client)
            .onSuccessTask(unused -> client.getLastLocation())
            .addOnFailureListener(e -> Log.d(TAG, "Location unavailable."));
}
```