Every data point in Google Fit has an associated data source. Data sources contain information that identifies the app or the device that collects or transforms the data. The package name of the app is available for data sources that don't represent a physical sensor.
Google Fit lets you do the following:
- Invoke an intent to view data that's associated with a specific app.
- Receive intents to show data using your app.
- Find out which app inserted a session. For more information, see Work with sessions.
Determine which app inserted a data point
To obtain the package name of the application that inserted a data point, first
call DataPoint.getOriginalDataSource
to get the data source, then call the
DataSource.getAppPackageName
method:
Kotlin
val dataPoint : DataPoint = ... val dataSource = dataPoint.originalDataSource val appPkgName = dataSource.appPackageName
Java
DataPoint dataPoint = ... DataSource dataSource = dataPoint.getOriginalDataSource(); String appPkgName = dataSource.getAppPackageName();
Receive intents from other apps
To register your app to receive intents from other health and wellness apps, declare an intent filter in your manifest that's similar to the following:
<intent-filter> <action android:name="vnd.google.fitness.VIEW" /> <data android:mimeType="vnd.google.fitness.data_type/com.google.step_count.cumulative" /> <data android:mimeType="vnd.google.fitness.data_type/com.google.step_count.delta" /> </intent-filter>
Each intent that your app receives from Google Fit is of only one type, but you can filter for multiple MIME types in a single intent filter. Your app's intent filter needs to include all of the data types that your app supports, including custom data types.
The fitness intents include the following extras:
vnd.google.gms.fitness.start_time
vnd.google.gms.fitness.end_time
vnd.google.gms.fitness.data_source
You can obtain data from these extras as follows:
Kotlin
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) ... val supportedType = DataType.getMimeType(DataType.TYPE_STEP_COUNT_DELTA) if (Intent.ACTION_VIEW == intent.action && supportedType == intent.type) { // Get the intent extras val startTime = Fitness.getStartTime(intent, TimeUnit.MILLISECONDS); val endTime = Fitness.getEndTime(intent, TimeUnit.MILLISECONDS) val dataSource = DataSource.extract(intent) } }
Java
@Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); ... String supportedType = DataType.getMimeType(DataType.TYPE_STEP_COUNT_DELTA); if (Intent.ACTION_VIEW.equals(getIntent().getAction()) && supportedType.equals(getIntent().getType()) { // Get the intent extras long startTime = Fitness.getStartTime(getIntent(), TimeUnit.MILLISECONDS); long endTime = Fitness.getEndTime(getIntent(), TimeUnit.MILLISECONDS); DataSource dataSource = DataSource.extract(getIntent()); } }
To obtain the MIME type for a custom data type, use the
MIME_TYPE_PREFIX
constant:
Kotlin
val supportedType = DataType.MIME_TYPE_PREFIX + "com.company.customdatatype"
Java
String supportedType = DataType.MIME_TYPE_PREFIX + "com.company.customdatatype";
Invoke an intent to view data
To invoke an intent to view data with another app, use the
HistoryApi.ViewIntentBuilder
class:
Kotlin
// Inside your activity val startTime = ... val endTime = ... val dataSource = ... val dataType = ... val fitIntent = 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 activity long startTime = ... long endTime = ... DataSource dataSource = ... DataType dataType = ... Intent fitIntent = new 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();
Learn more about how to use intents and intent filters.