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 background. More information on (Health Connect documentation)
Important: The SpikeSDK, 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.

Setup

Add the following permission to your AndroidManifest.xml:
<uses-permission android:name="android.permission.health.READ_HEALTH_DATA_IN_BACKGROUND" />
Check if background delivery is possible with current Health Connect version:
val hcAvailability = spikeConnection.getHealthConnectPermissionManager()
                    .isFeatureAvailable(HealthConnectFeature.READ_HEALTH_DATA_IN_BACKGROUND)
If not, you can ask user to update Health Connect to the latest version. Ask for background read permission:
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 in the same time as other permissions. Now you can enable background delivery:
connection.enableBackgroundDelivery(
    BackgroundDeliveryConfig(
        statisticsTypes = setOf(StatisticsType.STEPS)
    )
)
Keep in mind that calling enableBackgroundDeliverywill overwrite previous configuration. If you want to add more types, you have to call enableBackgroundDeliveryagain with updated configuration:
connection.enableBackgroundDelivery(
    BackgroundDeliveryConfig(
        statisticsTypes = setOf(StatisticsType.STEPS),
        activityTypes = setOf(ActivityType.RUNNING, ActivityType.WALKING)
    )
)
To check current configuration call getBackgroundDeliveryConfig() method. To stop background delivery call disableBackgroundDelivery() method.

Samsung Health

If Samsung Health Data is enabled before enabling background delivery, enableBackgroundDelivery will automatically enable Samsung Health Data background delivery.
Samsung Health Data is available on Android devices only.