记录健身数据

借助 Recording API,您的应用可以通过创建订阅,请求以省电的方式自动存储传感器数据。订阅与 Android 应用相关联,由健身数据类型或特定数据源组成。

您可以针对应用中的不同数据类型或数据源创建多个订阅。即使应用未运行,Google 健身也会存储有效订阅的健身数据,并在系统重启时恢复这些订阅。

记录的数据位于用户的健身记录中。如果您还想实时显示应用中的数据,请结合使用 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 健身会代表您的应用将 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.
        });
}