A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developers.google.com/android/guides/api-client below:

Access Google APIs | Google Play services

Skip to main content Access Google APIs

Stay organized with collections Save and categorize content based on your preferences.

To access APIs powered by Google Play services 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 in your app project.

Warning: APIs using GoogleApiClient 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 or an 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
// 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
// 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. If you are using an API that requires a GoogleSignInAccount object, use the AuthorizationResult#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().

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
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
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."));
}

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-08-14 UTC.

[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-14 UTC."],[[["To use Google Play services APIs, create API client objects which manage the connection and queue requests when offline."],["Access services without authorization by obtaining the client object using the current `Context` or `Activity`."],["Access services requiring authorization by signing in the user, requesting necessary permissions, and then getting the client object."],["Check for API availability using `checkApiAvailability()` if you need to disable features when an API is unavailable, although API calls will fail gracefully if the API isn't present."]]],["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"]]


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4