数据归因

Google 健身中的每个数据点都有关联的数据源。数据源包含用于识别收集或转换数据的应用或设备的信息。应用的软件包名称适用于不代表物理传感器的数据源。

借助 Google 健身,您可以执行以下操作:

  • 调用 Intent 以查看与特定应用关联的数据。
  • 接收 intent 以便使用应用显示数据。
  • 了解是哪个应用插入了会话。如需了解详情,请参阅使用会话

确定是哪个应用插入了数据点

如需获取插入数据点的应用的软件包名称,请先调用 DataPoint.getOriginalDataSource 以获取数据源,然后调用 DataSource.getAppPackageName 方法:

Kotlin

val dataPoint : DataPoint = ...
val dataSource = dataPoint.originalDataSource
val appPkgName = dataSource.appPackageName

Java

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

接收来自其他应用的 intent

如需注册您的应用以接收来自其他健康与健身应用的 intent,请在清单中声明类似于以下内容的 intent 过滤器:

<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>

您的应用从 Google 健身收到的每个 intent 都只有一个类型,但您可以在单个 intent 过滤器中过滤多个 MIME 类型。应用的 intent 过滤器需要包含应用支持的所有数据类型,包括自定义数据类型。

健身 intent 包括以下 extra:

  • vnd.google.gms.fitness.start_time
  • vnd.google.gms.fitness.end_time
  • vnd.google.gms.fitness.data_source

您可以从这些 extra 中获取数据,如下所示:

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());
    }
}

如需获取自定义数据类型的 MIME 类型,请使用 MIME_TYPE_PREFIX 常量:

Kotlin

val supportedType = DataType.MIME_TYPE_PREFIX + "com.company.customdatatype"

Java

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

调用 Intent 以查看数据

如需调用 intent 以查看其他应用的数据,请使用 HistoryApi.ViewIntentBuilder 类:

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();

详细了解如何使用 intent 和 intent 过滤器