Professional Documents
Culture Documents
Access Google APIs - Google Play Services - Google For Developers
Access Google APIs - Google Play Services - Google For Developers
Access Google APIs - Google Play Services - Google For Developers
When you want to make a call to one of the APIs in an SDK that's powered by Google
Play services, such as Google Sign-in or ML Kit, you need to first create an instance of
an API client object. These objects automatically manage the connection to Google
Play services. When a connection is available, each API client object executes
requests in order. Otherwise, the client object queues the requests. Unless
documentation indicates otherwise, client objects are cheap to construct; it's fine to
make new API clients every time you want to invoke API methods.
This guide shows how you can make API calls to any of the SDKs that are powered by
Google Play services, including how to access the services that don't require
authorization (#auth-not-required) and those that require authorization (#auth-required).
Get started
To get started, add the necessary tools and dependencies in your app project, as
described in the guide on how to set up Google Play services (/android/guides/setup).
For example, to get the device's last known location using the Fused Location Provider
for Android, add the logic that's shown in the following code snippet:
KotlinJava (#java)
(#kotlin)
The following example implements reading a user's daily steps using the Google Fit
API. To view a similar implementation in the context of a full project, view the main
activity of the BasicHistoryApiKotlin
(https://github.com/android/fit-
samples/blob/main/BasicHistoryApiKotlin/app/src/main/java/com/google/android/gms/fit/sam
ples/basichistoryapikotlin/MainActivity.kt)
app on GitHub.
KotlinJava (#java)
(#kotlin)
/*
* Checks whether the user is signed in. If so, executes the spe
* function. If the user is not signed in, initiates the sign-in
* specifying the function to execute after the user signs in.
*/
private fun fitSignIn() {
if (oAuthPermissionsApproved()) {
readDailySteps()
} else {
GoogleSignIn.requestPermissions(
this,
SIGN_IN_REQUEST_CODE,
getGoogleAccount(),
fitnessOptions
)
}
}
/*
* Gets a Google account for use in creating the fitness client.
* achieved by either using the last signed-in account, or if ne
* prompting the user to sign in. It's better to use the
* getAccountForExtension() method instead of the getLastSignedI
* method because the latter can return null if there has been n
* before.
*/
private fun getGoogleAccount(): GoogleSignInAccount =
GoogleSignIn.getAccountForExtension(requireContext(), fitnes
/*
* Handles the callback from the OAuth sign in flow, executing t
* after sign-in is complete.
*/
override fun onActivityResult(
requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (resultCode) {
RESULT_OK -> {
readDailySteps()
}
else -> {
// Handle error.
}
}
}
/*
* Reads the current daily step total.
*/
private fun readDailySteps() {
Fitness.getHistoryClient(requireContext(), getGoogleAccount(
.readDailyTotal(DataType.TYPE_STEP_COUNT_DELTA)
.addOnSuccessListener { dataSet ->
val total = when {
dataSet.isEmpty -> 0
else -> dataSet.dataPoints.first()
.getValue(Field.FIELD_STEPS).asInt()
}
companion object {
const val SIGN_IN_REQUEST_CODE = 1001
}
}
Note: This check is only necessary if you need to disable a feature in your app when an API is
unavailable. It's also fine to call API methods without performing this check first because the API
calls fail if the API is unavailable.
The following code snippet demonstrates how to check for the availability of the fused
location provider.
KotlinJava (#java)
(#kotlin)
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution
4.0 License (https://creativecommons.org/licenses/by/4.0/), and code samples are licensed under the
Apache 2.0 License (https://www.apache.org/licenses/LICENSE-2.0). For details, see the Google
Developers Site Policies (https://developers.google.com/site-policies). Java is a registered trademark
of Oracle and/or its affiliates.