Data Attribution

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 enables you to:

  • Determine which app inserted fitness data.
  • Fire an intent to view fitness data with a specific app.
  • Receive intents to show fitness data using your app.

For example, an app that analyzes a variety of data from the user's fitness history may want to present users with the list of fitness 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.

Determine which app inserted fitness data

To obtain the package name of the application that inserted a session, use the Session.getAppPackageName method:

Session session = ...;
String appPkgName = session.getAppPackageName();

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:

DataPoint dataPoint = ...;
DataSource dataSource = dataPoint.getOriginalDataSource();
String appPkgName = dataSource.getAppPackageName();

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 PackageManager class:

// Inside your activity
try {
    PackageManager pkgManager = this.getPackageManager();
    Drawable appIcon = pkgManager.getApplicationIcon(appPkgName);
    ...
} catch (PackageManager.NameNotFoundException e) {
    // The app is not installed on this device
    ...
}

Receive intents from fitness apps

To register your app to receive intents from other fitness 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:

void onCreate (Bundle savedInstanceState) {
    ...
    Intent intent = getIntent();
    String action = intent.getAction();
    String type = intent.getType();
    String supportedType = DataType.getMimeType(DataType.TYPE_STEP_COUNT_DELTA);

    if (Intent.ACTION_VIEW.equals(action) &&
        supportedType.compareTo(type) == 0) {

        // Get the intent extras
        long startTime = Fitness.getStartTime(intent, TimeUnit.MILLISECONDS);
        long endTime = Fitness.getEndTime(intent, TimeUnit.MILLISECONDS);
        DataSource dataSource = DataSource.extract(intent);
        Session 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:

String supportedType = DataType.MIME_TYPE_PREFIX + "com.company.customdatatype";

Fire an intent to view fitness data

To fire an intent to view fitness data with another app, use the HistoryApi.ViewIntentBuilder class.

// Inside your activity
long startTime = ...;
long endTime = ...;
DataSource dataSource = ...;

Intent fitIntent = new HistoryApi.ViewIntentBuilder(this)
    .setTimeInterval(startTime, endTime, TimeUnit.MILLISECONDS)
    .setDataSource(dataSource)
    .setPreferredApplication("com.example.app") // optional
    .build();

Fore more information about intents, see Intents and Intent Filters.

Send feedback about...