记录健身数据

借助 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.
        });
}