透過錄製 API,應用程式可透過建立訂閱的方式,要求以高效的方式自動儲存感應器資料。訂閱與 Android 應用程式相關聯,內含健身資料類型或特定資料來源。
您可以在應用程式中為不同的資料類型或資料來源建立多個訂閱項目。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. }); }