> ## Documentation Index
> Fetch the complete documentation index at: https://docs.spikeapi.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Android SDK Background Delivery

> Background delivery ensures that data updates are sent to your backend via webhooks, even when the application is in the background or closed.

### Important Notes About Background Delivery on Android

* Background delivery is scheduled to run every hour, but ultimately, Android decides when the delivery will be executed.
* Android may throttle the frequency of updates for background delivery depending on the app's activity, battery state, etc.
* Android may stop background delivery if it detects that the app is not active for a long time.
* There is a limit of queries that can be done in Health Connect, and it is different for foreground and background reads, so please request only essential data to be delivered in the background. More information in <a href="https://developer.android.com/health-and-fitness/guides/health-connect/plan/rate-limiting" target="_blank">Health Connect documentation</a>.

<Warning>
  **Important**: The Spike SDK, along with any other applications, cannot guarantee data synchronization on a fixed schedule. The hourly sync interval serves as a guideline rather than a strict requirement enforced by Android. Consequently, the actual synchronization frequency may vary, occurring hourly, once per day, or during specific system-defined events, such as the conclusion of Sleep Mode or when the device begins charging.
</Warning>

## Setup

Add the following permission to your \`AndroidManifest.xml:

```kotlin theme={null}
<uses-permission android:name="android.permission.health.READ_HEALTH_DATA_IN_BACKGROUND" />
```

Check if background delivery is possible with the current Health Connect version:

```kotlin theme={null}
val hcAvailability = spikeConnection.getHealthConnectPermissionManager()
                    .isFeatureAvailable(HealthConnectFeature.READ_HEALTH_DATA_IN_BACKGROUND)
```

If not, you can ask the user to update Health Connect to the latest version.

Ask for background read permission:

```kotlin theme={null}
val permissionManager = spikeConnection.getHealthConnectPermissionManager()
val requiredPermissions = permissionManager.getPermissions(
    // You can add other permissions as well: statistics, sleep, etc.
    includeBackgroundDelivery = true
)

val permissionLauncher = rememberLauncherForActivityResult(
    permissionManager.getRequestPermissionResultContract(),
    onResult = {
        ...
    }
)
permissionLauncher.launch(requiredPermissions)
```

You can also ask for background delivery permission at the same time as other permissions.

Now you can enable background delivery:

```kotlin theme={null}
connection.enableBackgroundDelivery(
    BackgroundDeliveryConfig(
        statisticsTypes = setOf(StatisticsType.STEPS)
    )
)
```

Keep in mind that calling `enableBackgroundDelivery` will overwrite the previous configuration. If you want to add more types, you have to call `enableBackgroundDelivery` again with updated configuration:

```kotlin theme={null}
connection.enableBackgroundDelivery(
    BackgroundDeliveryConfig(
        statisticsTypes = setOf(StatisticsType.STEPS),
        activityTypes = setOf(ActivityType.RUNNING, ActivityType.WALKING)
    )
)
```

To check the current configuration, call the `getBackgroundDeliveryConfig()` method.

To stop background delivery, call the `disableBackgroundDelivery()` method.

## Samsung Health

If Samsung Health Data is enabled before enabling background delivery, `enableBackgroundDelivery` will automatically enable Samsung Health Data background delivery.
