![](https://fonts.gstatic.com/s/i/productlogos/travel_hotels/v4/192px.svg)

# Help users understand their sleep habits

The Sleep API, a library powered by [Google Play services](https://developers.google.com/android.md.txt) allows apps to determine when the user goes to sleep and wakes up.

After receiving permission from the user, Google Play services collects information related to surrounding brightness, device movement, and more to infer the times when the user falls asleep and wakes up. Your app can subscribe to updates to this information. That way, your app can inform users about their sleep habits and help encourage users to improve their sleep hygiene and overall well-being.

## Before you begin

To prepare your app, complete the steps in the following sections.

### App prerequisites

Make sure that your app's build file uses the following values:

- A `minSdkVersion` of `29` or higher.
- A `compileSdkVersion` of `29` or higher.

.

## Configure your app

In your project-level `build.gradle` file, include [Google's Maven repository](https://maven.google.com/web/index.html) and [Maven central repository](https://search.maven.org/artifact) in both your `buildscript` and `allprojects` sections:

```groovy
 buildscript {
    repositories {
        google()
        mavenCentral()
    }
} 

 allprojects {
    repositories {
        google()
        mavenCentral()
    }
} 
```

Add the [Google Play services](https://developers.google.com/android.md.txt) dependency for the Sleep API to your [module's Gradle build file](https://developer.android.com/studio/build#module-level), which is commonly `app/build.gradle`:

```groovy
 dependencies {
    implementation 'com.google.android.gms:play-services-location:25.0.0'
} 
```

Add the [ACTIVITY_RECOGNITION](https://developer.android.com/reference/android/Manifest.permission#ACTIVITY_RECOGNITION) permission to your `AndroidManifest.xml` tag with `android:name="android.permission.ACTIVITY_RECOGNITION"`.

```xml
 <manifest>
 <uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />
 <application>...</application>
 </manifest> 
```

## Register for sleep updates

Prior to registering for updates, first check that the user has granted the `ACTIVITY_RECOGNITION` permission. For more information about permissions, see [Request App Permissions](https://developer.android.com/training/permissions/requesting).

Once permission has been granted, register for updates to the user's sleep behavior, including sleep segments and sleep event classification results, by calling [`requestSleepSegmentUpdates()`]().

```kotlin
 val task = ActivityRecognition.getClient(context)
    .requestSleepSegmentUpdates(
        pendingIntent,
        SleepSegmentRequest.getDefaultSleepSegmentRequest())
    .addOnSuccessListener {
        viewModel.updateSubscribedToSleepData(true)
        Log.d(TAG, "Successfully subscribed to sleep data.")
    }
    .addOnFailureListener { exception ->
        Log.d(TAG, "Exception when subscribing to sleep data: $exception")
    }
```

## Learn more about the Sleep API

### [Receive a daily summary of sleep time](http://developers.google.com/android/reference/com/google/android/gms/location/SleepSegmentEvent.md.txt)

Your app can retrieve information about sleep time from the daily sleep segment update event.

Each sleep segment event contains information about whether the API detected sleep or could detect sleep. The segment event also includes the times when the user most likely fell asleep and woke up, based on available sensor data.

### [Stay updated on the likelihood that the user is asleep](http://developers.google.com/android/reference/com/google/android/gms/location/SleepClassifyEvent.md.txt)

Your app can also receive regular updates about sleep classification events.

Each sleep classification event provides a timestamp, along with values that represent device motion, environment brightness, and the likelihood that the user is asleep at that time. For example, if the sleep confidence significantly increases between one timestamp and the next timestamp, and if the surrounding brightness significantly decreases between those same 2 timestamps, then it's likely that the user recently fell asleep.

Your app can combine this information with additional user-provided data to more confidently determine when the user falls asleep and wakes up.

### [See the API in action](https://github.com/android/location-samples/tree/main/SleepSampleKotlin)

The SleepSampleKotlin sample app demonstrates an end-to-end workflow using the Sleep API.