資料歸因

Google Fit 中的每個資料點都有相關聯的資料來源。資料來源包含的資訊可識別應用程式或收集或轉換資料的裝置。應用程式的套件名稱適用於沒有代表實體感應器的資料來源時。

Google Fit 可讓您執行下列操作:

  • 叫用意圖,以便查看與特定應用程式相關的資料。
  • 接收意圖,以便使用應用程式顯示資料。
  • 找出插入工作階段的應用程式。詳情請參閱使用工作階段

判斷哪個應用程式插入了資料點

如要取得插入資料點的應用程式套件名稱,請先呼叫 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-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 Fit 收到的每個意圖只是一種類型,但您可以在單一意圖篩選器中篩選多個 MIME 類型。應用程式的意圖篩選器必須包含應用程式支援的所有資料類型,包括自訂資料類型。

健身意圖包含下列額外項目:

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

您可以透過以下額外方式取得額外資料:

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

叫用意圖來查看資料

如要叫用意圖來查看其他應用程式的資料,請使用 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();

進一步瞭解如何使用意圖和意圖篩選器