記錄健身資料

應用程式可透過 Recording API,將感應器資料自動儲存在 以節省電力已與訂閱項目建立關聯 含有健身資料類型或特定資料 來源。

您可以針對不同資料類型或資料來源建立多個訂閱項目 。Google Fit 會儲存有效訂閱項目的健身資料 而且即使應用程式沒有執行,只要這些訂閱就會還原 系統就會重新啟動

記錄的資料會顯示在使用者的健身記錄中。如果您也想要 如要在應用程式中即時顯示資料,請使用 Sensors API 與 Recording 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.
        });
}