Skip to main content

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 Health Connect documentation.
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.

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 the current Health Connect version:
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:
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:
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:
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.