How Sahha's Background Tasks Work in Android

In the Android operating system, background tasks must provide some form of visibility to the user, so the main method of adhering to that is by using what’s called a foreground service . This is essentially a visible task, in the form of a silent/persistent notification that describes that something is happening. Sahha just lets the user know that it’s Gathering health insights in its foreground service by default but this may be adjusted when you configure the SDK (Adjusting the notification settings).

Within this service lies the main collection task of, detecting steps using Android’s Activity Recognition API and screen locks from the phone. It also periodically checks any new data has come through from HealthConnect if it is available on the device (every 15 minutes), which adds greater value to the health data integration.

In addition to the main service, the SDK uses various Workers — Android’s power efficient background tasks — to post the data to our database and collect sleep data from the device, focusing on the ability to capture behavioral health insights even if the user only has a phone.

Here is a visual example on how Sahha’s Android background tasks work:





Main Components of Sahha's Background Tasks

  1. Foreground Service:
    • Purpose: Provides visibility for background tasks.
    • Functionality: Detects steps, screen locks, and periodically queries HealthConnect data.
  2. Workers:
    • Sleep Worker: Collects and processes sleep data from the device.
    • Post Worker: Posts all collected data to the Sahha database every 15 minutes.
    • HealthConnect Post Worker: Posts HealthConnect data to the Sahha database every 15 minutes.

To help ensure that Sahha’s health insights are accurate and reliable, the SDK uses several strategies, making it a robust solution for an Android health API:

  • Foreground Service: Ensures continuous data collection even when the app is not in use.
  • Efficient Workers: Utilizes Android’s WorkManager to schedule and manage background tasks efficiently.
  • HealthConnect Integration: Periodically checks and integrates new data from HealthConnect in addition to the device data, enhancing the comprehensiveness of health insights.

Sahha’s approach to managing Android background tasks ensures that health data is collected continuously and efficiently, even when the app is not actively in use.

By leveraging Google’s Activity Recognition API and Health Connect, Sahha provides a broad range of health insights. Whether you're a developer looking to integrate a fitness app to our API or a company seeking a robust health tracking solution, Sahha’s SDK offers a powerful and adaptable platform to meet your needs.