Every data point in Google Fit has an associated data source. Data sources contain information to identify 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:
- Find out which app inserted a session.
- Fire an intent to view data with a specific app.
- Receive intents to show data using your app.
For example, an app that analyzes a variety of data from the user's history may want to present users with the list of sessions included in the analysis, along with information about which app inserted each session. When users tap on each session, the app can fire an intent to show a detailed view of the session using a different app.
Find which app inserted a session
To obtain the package name of the application that inserted a session, use the
Session.getAppPackageName
method:
val session : Session = ... val appPkgName = session.appPackageName
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:
val dataPoint : DataPoint = ... val dataSource = dataPoint.dataSource val appPkgName = dataSource.appPackageName
Get more information about an app
To determine whether an Android app is installed on the device and obtain more information about
the app (such as its icon), use the Android
tPackageManager
class:
// Inside your activity try { val appIcon = packageManager.getApplicationIcon(appPkgName) } catch (e: PackageManager.NameNotFoundException) { // The app is not installed on this device ... }
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 like 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. You should include all the data types that your app supports in your intent filter, including custom data types.
The fitness intents include all of the following extras:
"com.google.gms.fitness.start_time"
"com.google.gms.fitness.end_time"
"com.google.gms.fitness.data_source"
"com.google.gms.fitness.session"
You can obtain data from these extras as follows:
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); val session = Session.extract(intent); // Show the session in your app ... } }
To obtain the MIME type for a custom data type, use the
MIME_TYPE_PREFIX
constant:
val supportedType = DataType.MIME_TYPE_PREFIX + "com.company.customdatatype";
Fire an intent to view data
To fire an intent to view data with another app, use the
HistoryApi.ViewIntentBuilder
class.
// 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 .build()
Fore more information about intents, see Intents and Intent Filters.