通过 Recording API,您的应用可以请求在 从而节省电池电量关联了一项订阅 包含健身数据类型或特定数据 来源。
您可以为不同的数据类型或数据源创建多个订阅 。Google 健身会存储来自有效订阅、 即使您的应用未运行,也可以恢复这些订阅, 系统重启。
所记录的数据会在用户的健身记录中提供。如果您还希望 在应用中实时显示数据,请使用 传感器 API 与 Recording API 结合使用。录制 包含课程元数据的健身数据,请使用 Session 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 健身会存储健身数据
类型的数据
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. }); }