Le API Google Fit, inclusa l'API REST di Google Fit, verranno ritirate nel 2026. A partire dal 1° maggio 2024, gli sviluppatori non possono registrarsi per utilizzare queste API.
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Le autorizzazioni Android hanno lo scopo di proteggere la privacy di un utente Android. Richiedono
L'utente quando le app vogliono accedere a tipi di dati per dati sensibili come contatti o foto.
di sistema come il rilevamento della posizione o dei passi. Gli utenti concedono queste autorizzazioni
al primo download dell'app.
Se la tua app ha bisogno di accedere a uno di questi tipi di dati
(un sottoinsieme dei tipi di dati di Google Fit), richiedi l'autorizzazione Android pertinente prima di richiedere le autorizzazioni OAuth.
Consulta quanto riportato di seguito.
Figura 1.La schermata per il consenso delle autorizzazioni Android.
Tipi di dati che richiedono le autorizzazioni Android
Per accedere ai tipi di dati sopra indicati con le API Google Fit, devi implementare la logica per gestire la richiesta delle autorizzazioni Android sia per Android 10 sia per le versioni precedenti di Android. Questi esempi utilizzano l'autorizzazione ACTIVITY_RECOGNITION.
Android 10
In questo modo, la tua app può avere come target il livello API 29 o versioni successive, richiedere l'autorizzazione all'utente e registrarla nel file manifest dell'applicazione.
if(ContextCompat.checkSelfPermission(thisActivity,Manifest.permission.ACTIVITY_RECOGNITION)!=PackageManager.PERMISSION_GRANTED){// Permission is not granted}
Se l'autorizzazione non è già stata concessa, richiedila:
[null,null,["Ultimo aggiornamento 2025-08-31 UTC."],[[["\u003cp\u003eAndroid permissions protect user privacy by requiring apps to request access to sensitive data and system features.\u003c/p\u003e\n"],["\u003cp\u003eApps need to request specific Android permissions before accessing certain Google Fit data types, such as activity, location, and heart rate data.\u003c/p\u003e\n"],["\u003cp\u003eDifferent data types require different permissions, like \u003ccode\u003eACTIVITY_RECOGNITION\u003c/code\u003e for step count and activity data, \u003ccode\u003eACCESS_FINE_LOCATION\u003c/code\u003e for location data, and \u003ccode\u003eBODY_SENSORS\u003c/code\u003e for heart rate data.\u003c/p\u003e\n"],["\u003cp\u003eRequesting Android permissions involves adding them to the manifest file and implementing logic to handle permission requests for different Android versions.\u003c/p\u003e\n"],["\u003cp\u003eApps targeting older Android versions should be updated to handle potential permission revocations by users on Android 10 and above.\u003c/p\u003e\n"]]],[],null,["# Android Permissions\n\n[Android permissions](https://developer.android.com/guide/topics/permissions/overview) aim to protect the privacy of an Android user. They prompt\nthe user when apps want to access data types for sensitive data like contacts or photos, and\nsystem features like location or step detection. Users grant these permissions\nwhen first downloading your app.\n\nIf your app needs access to any of [these data types](#data_types_that_need_android_permissions)\n(a subset of the Google Fit data types), request the relevant\nAndroid permission before requesting the OAuth permissions.\n[See below](#requesting_android_permissions).\n**Figure 1.**The Android permissions consent screen.\n\n*** ** * ** ***\n\nData types that need Android permissions\n----------------------------------------\n\nTo access these physical activity data types, you'll need to [request the `ACTIVITY_RECOGNITION` Android permission](#requesting_android_permissions):\n\n- To [record](/android/reference/com/google/android/gms/fitness/RecordingClient) these data types:\n\n - `com.google.step_count.delta`\n - `com.google.step_count.cumulative`\n - `com.google.step_count.cadence`\n - `com.google.activity.segment`\n - `com.google.calories.expended`\n- To [read](/android/reference/com/google/android/gms/fitness/HistoryClient) these data types:\n\n - `com.google.step_count.delta`\n - `com.google.step_count.cumulative`\n - `com.google.step_count.cadence`\n - `com.google.activity.segment`\n - `com.google.activity.exercise`\n\nTo access these data types, you'll need to [request the `ACCESS_FINE_LOCATION` Android permission](https://developer.android.com/reference/android/Manifest.permission.html#ACCESS_FINE_LOCATION):\n\n- To read these data types:\n - `com.google.distance.delta`\n - `com.google.location.sample`\n - `com.google.location.bounding_box`\n - `com.google.speed`\n\nTo [record](/android/reference/com/google/android/gms/fitness/RecordingClient) these data types, you'll need to [request the `BODY_SENSORS` Android permission](https://developer.android.com/reference/android/Manifest.permission.html#BODY_SENSORS):\n\n- `com.google.heart_rate.bpm`\n\nRequesting Android permissions\n------------------------------\n\nLearn about [requesting Android permissions](https://developer.android.com/training/permissions/requesting), the\n[physical activity recognition permission](https://developer.android.com/about/versions/10/privacy/changes#physical-activity-recognition), the\n[fine location permission](https://developers.google.com/maps/documentation/android-sdk/location#location_permissions), and the [body sensors permission](https://developer.android.com/reference/android/Manifest.permission.html#BODY_SENSORS).\n\nTo access the data types above with the Google Fit APIs, you'll need to\nimplement logic to handle requesting Android permissions for both Android 10 and\nprevious versions of Android. These examples use the [`ACTIVITY_RECOGNITION`](https://developer.android.com/reference/android/Manifest.permission#ACTIVITY_RECOGNITION) permission. \n\n### Android 10\n\nSo your app can target API level 29 or above, request the permission from the\nuser, and register the permission in the application manifest file.\n\n1. [Add the permission to the manifest file](https://developer.android.com/training/permissions/requesting#perm-add).\n\n \u003cuses-permission android:name=\"android.permission.ACTIVITY_RECOGNITION\"/\u003e\n\n2. [Check if the permission is granted](https://developer.android.com/training/permissions/requesting#perm-check):\n\n if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.ACTIVITY_RECOGNITION)\n != PackageManager.PERMISSION_GRANTED) {\n // Permission is not granted\n }\n\n3. If permission isn't already granted, request the permission:\n\n ActivityCompat.requestPermissions(thisActivity,\n arrayOf(Manifest.permission.ACTIVITY_RECOGNITION),\n MY_PERMISSIONS_REQUEST_ACTIVITY_RECOGNITION)\n\n### Android 9 and below\n\nSo your app can target API level 28 or below:\n\n1. Request the `com.google.android.gms.permission.ACTIVITY_RECOGNITION` permission.\n\n2. [Add the permission to the manifest file](https://developer.android.com/training/permissions/requesting#perm-add).\n\n \u003cuses-permission android:name=\"android.gms.permission.ACTIVITY_RECOGNITION\"/\u003e\n\n| **Caution:** If your app targets API level 28 or below but is running on Android 10, the `com.google.android.gms.permission.ACTIVITY_RECOGNITION` permission is converted into a pre-granted runtime permission which can be revoked by the user. If revoked, your app's access to these [physical activity\n| data types](#data_types_that_need_android_permissions) will be blocked. Update your app to handle this situation."]]