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.
Ogni punto dati in Google Fit ha un'origine dati associata. Origini dati
Contenere informazioni che identificano l'app o il dispositivo che raccoglie o
trasforma i dati. Il nome del pacchetto dell'app è disponibile per le origini dati
che non rappresentano un sensore fisico.
Google Fit ti consente di:
Richiama un intent per visualizzare i dati associati a un'app specifica.
Ricevere intent per mostrare i dati utilizzando la tua app.
Per registrare la tua app in modo da ricevere intent da altre app per la salute e il benessere,
dichiarare un filtro per intent nel manifest simile al seguente:
Ogni intent che la tua app riceve da Google Fit è di un solo tipo,
ma puoi filtrare per più tipi MIME in un singolo filtro per intent. Il
filtro intent dell'app deve includere tutti i tipi di dati supportati dall'app,
inclusi i tipi di dati personalizzati.
Gli scopi di fitness includono i seguenti extra:
vnd.google.gms.fitness.start_time
vnd.google.gms.fitness.end_time
vnd.google.gms.fitness.data_source
Puoi ottenere i dati da questi extra nel seguente modo:
Kotlin
overridefunonCreate(savedInstanceState:Bundle?){super.onCreate(savedInstanceState)...valsupportedType=DataType.getMimeType(DataType.TYPE_STEP_COUNT_DELTA)if(Intent.ACTION_VIEW==intent.action && supportedType==intent.type){// Get the intent extrasvalstartTime=Fitness.getStartTime(intent,TimeUnit.MILLISECONDS);valendTime=Fitness.getEndTime(intent,TimeUnit.MILLISECONDS)valdataSource=DataSource.extract(intent)}}
Java
@OverridepublicvoidonCreate(@NullableBundlesavedInstanceState){super.onCreate(savedInstanceState);...StringsupportedType=DataType.getMimeType(DataType.TYPE_STEP_COUNT_DELTA);if(Intent.ACTION_VIEW.equals(getIntent().getAction()) && supportedType.equals(getIntent().getType()){// Get the intent extraslongstartTime=Fitness.getStartTime(getIntent(),TimeUnit.MILLISECONDS);longendTime=Fitness.getEndTime(getIntent(),TimeUnit.MILLISECONDS);DataSourcedataSource=DataSource.extract(getIntent());}}
Per ottenere il tipo MIME per un tipo di dati personalizzato, utilizza la costante
MIME_TYPE_PREFIX:
Per richiamare un intent per visualizzare i dati con un'altra app, utilizza la classe
HistoryApi.ViewIntentBuilder
classe:
Kotlin
// Inside your activityvalstartTime=...valendTime=...valdataSource=...valdataType=...valfitIntent=HistoryApi.ViewIntentBuilder(this,dataType).setTimeInterval(startTime,endTime,TimeUnit.MILLISECONDS).setDataSource(dataSource)// Optional if a specific data source is desired.setPreferredApplication("com.example.app")// Optional if you'd like a// specific app to handle the intent if that app is installed on the device.build()
Java
// Inside your activitylongstartTime=...longendTime=...DataSourcedataSource=...DataTypedataType=...IntentfitIntent=newHistoryApi.ViewIntentBuilder(this,dataType).setTimeInterval(startTime,endTime,TimeUnit.MILLISECONDS).setDataSource(dataSource)// Optional if a specific data source is desired.setPreferredApplication("com.example.app")// Optional if you'd like a// specific app to handle the intent if that app is installed on the device.build();
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Mancano le informazioni di cui ho bisogno","missingTheInformationINeed","thumb-down"],["Troppo complicato/troppi passaggi","tooComplicatedTooManySteps","thumb-down"],["Obsoleti","outOfDate","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Problema relativo a esempi/codice","samplesCodeIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 2025-08-31 UTC."],[[["\u003cp\u003eGoogle Fit allows developers to identify the source of fitness data, including the app or device that collected it.\u003c/p\u003e\n"],["\u003cp\u003eApps can be registered to receive intents from other health apps, enabling them to display and utilize fitness data.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers can initiate intents to view specific fitness data within other apps, fostering interoperability between health platforms.\u003c/p\u003e\n"],["\u003cp\u003eData sources in Google Fit contain identifying information, including package names for apps, and are linked to individual data points.\u003c/p\u003e\n"]]],[],null,["# Data attribution\n\nEvery data point in Google Fit has an associated data source. Data sources\ncontain information that identifies the app or the device that collects or\ntransforms the data. The package name of the app is available for data sources\nthat don't represent a physical sensor.\n\nGoogle Fit lets you do the following:\n\n- Invoke an intent to view data that's associated with a specific app.\n- Receive intents to show data using your app.\n- Find out which app inserted a [session](/fit/sessions). For more information, see [Work with sessions](/fit/android/using-sessions).\n\nDetermine which app inserted a data point\n-----------------------------------------\n\nTo obtain the package name of the application that inserted a data point, first\ncall [`DataPoint.getOriginalDataSource`](/android/reference/com/google/android/gms/fitness/data/DataPoint#public-datasource-getoriginaldatasource)\nto get the data source, then call the\n[`DataSource.getAppPackageName`](/android/reference/com/google/android/gms/fitness/data/DataSource#public-string-getapppackagename)\nmethod: \n\n### Kotlin\n\n```kotlin\nval dataPoint : DataPoint = ...\nval dataSource = dataPoint.originalDataSource\nval appPkgName = dataSource.appPackageName\n```\n\n### Java\n\n```java\nDataPoint dataPoint = ...\nDataSource dataSource = dataPoint.getOriginalDataSource();\nString appPkgName = dataSource.getAppPackageName();\n```\n\nReceive intents from other apps\n-------------------------------\n\nTo register your app to receive intents from other health and wellness apps,\ndeclare an intent filter in your manifest that's similar to the following: \n\n```xml\n\u003cintent-filter\u003e\n \u003caction android:name=\"vnd.google.fitness.VIEW\" /\u003e\n \u003cdata android:mimeType=\"vnd.google.fitness.data_type/com.google.step_count.cumulative\" /\u003e\n \u003cdata android:mimeType=\"vnd.google.fitness.data_type/com.google.step_count.delta\" /\u003e\n\u003c/intent-filter\u003e\n```\n| **Note:** Don't include the `DEFAULT` category in your intent filter, unless you want your app to receive intents for data created by other apps.\n\nEach intent that your app receives from Google Fit is of only one type,\nbut you can filter for multiple MIME types in a single intent filter. Your app's\nintent filter needs to include all of the data types that your app supports,\nincluding custom data types.\n\nThe fitness intents include the following extras:\n\n- `vnd.google.gms.fitness.start_time`\n- `vnd.google.gms.fitness.end_time`\n- `vnd.google.gms.fitness.data_source`\n\nYou can obtain data from these extras as follows: \n\n### Kotlin\n\n```kotlin\noverride fun onCreate(savedInstanceState: Bundle?) {\n super.onCreate(savedInstanceState)\n ...\n val supportedType = DataType.getMimeType(DataType.TYPE_STEP_COUNT_DELTA)\n\n if (Intent.ACTION_VIEW == intent.action && supportedType == intent.type) {\n // Get the intent extras\n val startTime = Fitness.getStartTime(intent, TimeUnit.MILLISECONDS);\n val endTime = Fitness.getEndTime(intent, TimeUnit.MILLISECONDS)\n val dataSource = DataSource.extract(intent)\n }\n}\n```\n\n### Java\n\n```java\n@Override\npublic void onCreate(@Nullable Bundle savedInstanceState) {\n super.onCreate(savedInstanceState);\n ...\n String supportedType = DataType.getMimeType(DataType.TYPE_STEP_COUNT_DELTA);\n\n if (Intent.ACTION_VIEW.equals(getIntent().getAction()) && supportedType.equals(getIntent().getType())\n {\n // Get the intent extras\n long startTime = Fitness.getStartTime(getIntent(), TimeUnit.MILLISECONDS);\n long endTime = Fitness.getEndTime(getIntent(), TimeUnit.MILLISECONDS);\n DataSource dataSource = DataSource.extract(getIntent());\n }\n}\n```\n\nTo obtain the MIME type for a custom data type, use the\n[`MIME_TYPE_PREFIX`](/android/reference/com/google/android/gms/fitness/data/DataType#public-static-final-string-mime_type_prefix)\nconstant: \n\n### Kotlin\n\n```kotlin\nval supportedType = DataType.MIME_TYPE_PREFIX + \"com.company.customdatatype\"\n```\n\n### Java\n\n```java\nString supportedType = DataType.MIME_TYPE_PREFIX + \"com.company.customdatatype\";\n```\n\nInvoke an intent to view data\n-----------------------------\n\nTo invoke an intent to view data with another app, use the\n[`HistoryApi.ViewIntentBuilder`](/android/reference/com/google/android/gms/fitness/HistoryApi.ViewIntentBuilder)\nclass: \n\n### Kotlin\n\n```kotlin\n// Inside your activity\nval startTime = ...\nval endTime = ...\nval dataSource = ...\nval dataType = ...\n\nval fitIntent = HistoryApi.ViewIntentBuilder(this, dataType)\n .setTimeInterval(startTime, endTime, TimeUnit.MILLISECONDS)\n .setDataSource(dataSource) // Optional if a specific data source is desired\n .setPreferredApplication(\"com.example.app\") // Optional if you'd like a\n // specific app to handle the intent if that app is installed on the device\n .build()\n```\n\n### Java\n\n```java\n// Inside your activity\nlong startTime = ...\nlong endTime = ...\nDataSource dataSource = ...\nDataType dataType = ...\n\nIntent fitIntent = new HistoryApi.ViewIntentBuilder(this, dataType)\n .setTimeInterval(startTime, endTime, TimeUnit.MILLISECONDS)\n .setDataSource(dataSource) // Optional if a specific data source is desired\n .setPreferredApplication(\"com.example.app\") // Optional if you'd like a\n // specific app to handle the intent if that app is installed on the device\n .build();\n```\n\nLearn more about how to use [intents and intent\nfilters](https://developer.android.com/guide/components/intents-filters)."]]