# Record fitness data

The Recording API lets your app request automated storage of sensor data in a
battery-efficient manner by creating subscriptions. A subscription is associated
with an Android app and consists of a fitness data type or a specific data
source.

You can create multiple subscriptions for different data types or data sources
in your app. Google Fit stores fitness data from active subscriptions,
even when your app isn't running, and it restores these subscriptions when the
system restarts.

The recorded data is available in the user's fitness history. If you also want
to show the data in your app in real time, use the
[Sensors API](https://developers.google.com/fit/android/sensors) together with the Recording API. To record
fitness data with session metadata, use the
[Sessions API](https://developers.google.com/fit/android/using-sessions).

> [!NOTE]
> **Note:** For best practices when you manage user data, see [Google Fit Developer and User Data Policy](https://developers.google.com/fit/policy).

## Subscribe to fitness data

To request background collection of sensor data in your app, use the
[`RecordingClient.subscribe`](https://developers.google.com/android/reference/com/google/android/gms/fitness/RecordingClient)
method, as shown in the following code snippet:

### Kotlin

```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

```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));
```

If the subscription is added successfully, Google Fit stores fitness
data of type
[`TYPE_STEP_COUNT_DELTA`](https://developers.google.com/android/reference/com/google/android/gms/fitness/data/DataType#TYPE_STEP_COUNT_DELTA)
in the fitness history on behalf of your app. This subscription appears in the
list of active subscriptions for your app.

To subscribe to more types of fitness data in your app, follow the steps in the
previous example but provide a different fitness data type each time.

> [!NOTE]
> **Note:** Apps that run on Android 10 (API level 29) or higher must [get an Android permission](https://developers.google.com/fit/android/authorization#requesting_android_permissions) when they record some physical activity data types.

## List active subscriptions

To get a list of the active subscriptions for your app, use the
[`RecordingClient.listSubscriptions`](https://developers.google.com/android/reference/com/google/android/gms/fitness/RecordingClient)
method, as shown in the following code snippet:

### Kotlin

```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

```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}");
            }
    });
}
```

## Unsubscribe from fitness data

To stop the collection of sensor data in your app, use the
[`RecordingClient.unsubscribe`](https://developers.google.com/android/reference/com/google/android/gms/fitness/RecordingClient)
method, as shown in the following code snippet:

### Kotlin

```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

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