記錄健身資料

錄製 API 可讓應用程式透過建立訂閱的方式,自動以高效的方式儲存感應器資料。訂閱與 Android 應用程式相關聯,內含健身資料類型或特定資料來源。

您可以為應用程式中不同類型的資料類型或資料來源建立多個訂閱項目。即使應用程式並未執行,Google Fit 仍會儲存有效訂閱項目的健身資料,並在系統重新啟動時還原這些訂閱項目。

使用者的記錄資料會顯示在使用者的健身記錄中。如要同時即時顯示應用程式資料,請搭配使用 Sensors API 與錄製 API。如要使用工作階段中繼資料記錄健身資料,請使用 Sessions API

訂閱健身資料

如要在應用程式中收集感應器資料的背景收集行為,請使用 RecordingClient.subscribe 方法,如以下程式碼片段所示:

Kotlin

Fitness.getRecordingClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions))
    // This example shows subscribing to a DataType, across all possible data
    // sources. Alternatively, a specific DataSource can be used.
    .subscribe(DataType.TYPE_STEP_COUNT_DELTA)
    .addOnSuccessListener {
        Log.i(TAG, "Successfully subscribed!")
    }
    .addOnFailureListener { e ->
        Log.w(TAG, "There was a problem subscribing.", e)
    }

Java

Fitness.getRecordingClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions))
        // This example shows subscribing to a DataType, across all possible
        // data sources. Alternatively, a specific DataSource can be used.
        .subscribe(DataType.TYPE_STEP_COUNT_DELTA)
        .addOnSuccessListener(unused ->
                Log.i(TAG, "Successfully subscribed!"))
        .addOnFailureListener( e ->
        Log.w(TAG, "There was a problem subscribing.", e));

如果成功新增訂閱項目,Google Fit 會代表您的應用程式在健身記錄中儲存 TYPE_STEP_COUNT_DELTA 類型的健身資料。這個訂閱項目會顯示在應用程式的有效訂閱項目清單中。

如要訂閱應用程式中更多的健身資料,請按照上一個範例中的步驟,但每次都要提供不同的健身資料類型。

列出有效訂閱

如需應用程式的有效訂閱項目清單,請使用 RecordingClient.listSubscriptions 方法,如以下程式碼片段所示:

Kotlin

Fitness.getRecordingClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions))
    .listSubscriptions()
    .addOnSuccessListener { subscriptions ->
        for (sc in subscriptions) {
            val dt = sc.dataType
            Log.i(TAG, "Active subscription for data type: ${dt.name}")
        }
    }

Java

Fitness.getRecordingClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions))
        .listSubscriptions()
        .addOnSuccessListener(subscriptions -> {
            for (Subscription sc : subscriptions) {
                DataType dt = sc.getDataType();
                Log.i(TAG, "Active subscription for data type: ${dt.name}");
            }
    });
}

取消訂閱健身資料

如要停止收集感應器資料,請使用 RecordingClient.unsubscribe 方法,如以下程式碼片段所示:

Kotlin

Fitness.getRecordingClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions))
    // This example shows unsubscribing from a DataType. A DataSource should
    // be used where the subscription was to a DataSource. Alternatively, if
    // the client doesn't maintain its subscription information, they can use
    // an element from the return value of listSubscriptions(), which is of
    // type Subscription.
    .unsubscribe(DataType.TYPE_STEP_COUNT_DELTA)
    .addOnSuccessListener {
        Log.i(TAG,"Successfully unsubscribed.")
    }
    .addOnFailureListener { e->
        Log.w(TAG, "Failed to unsubscribe.")
        // Retry the unsubscribe request.
    }

Java

Fitness.getRecordingClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions))
        // This example shows unsubscribing from a DataType. A DataSource
        // should be used where the subscription was to a DataSource.
        // Alternatively, if the client doesn’t maintain its subscription
        // information, they can use an element from the return value of
        // listSubscriptions(), which is of type Subscription.
        .unsubscribe(DataType.TYPE_STEP_COUNT_DELTA)
        .addOnSuccessListener(unused ->
            Log.i(TAG,"Successfully unsubscribed."))
        .addOnFailureListener(e -> {
            Log.w(TAG, "Failed to unsubscribe.");
            // Retry the unsubscribe request.
        });
}