androidx.wear.ambient

Interfaces

AmbientLifecycleObserver

Interface for LifecycleObservers which are used to add ambient mode support to activities on Wearable devices.

AmbientLifecycleObserver.AmbientLifecycleCallback

Callback to receive ambient mode state changes.

AmbientMode.AmbientCallbackProvider

Interface for any Activity that wishes to implement Ambient Mode.

AmbientModeSupport.AmbientCallbackProvider

Interface for any Activity that wishes to implement Ambient Mode.

Classes

AmbientLifecycleObserver.AmbientDetails

Details about ambient mode support on the current device, passed to AmbientLifecycleCallback.onEnterAmbient.

AmbientMode

This class is deprecated.

Use AmbientLifecycleObserver instead.

AmbientMode.AmbientCallback

Callback to receive ambient mode state changes.

AmbientMode.AmbientController

A class for interacting with the ambient mode on a wearable device.

AmbientModeSupport

This class is deprecated.

Use AmbientLifecycleObserver instead.

AmbientModeSupport.AmbientCallback

Callback to receive ambient mode state changes.

AmbientModeSupport.AmbientController

A class for interacting with the ambient mode on a wearable device.

Top-level functions summary

AmbientLifecycleObserver

Create a new AmbientLifecycleObserver for use on a real device.

AmbientLifecycleObserver
AmbientLifecycleObserver(
    activity: Activity,
    callbackExecutor: Executor,
    callbacks: AmbientLifecycleObserver.AmbientLifecycleCallback
)

Create a new AmbientLifecycleObserver for use on a real device.

Top-level functions

AmbientLifecycleObserver

Added in 1.3.0
fun AmbientLifecycleObserver(
    activity: Activity,
    callbacks: AmbientLifecycleObserver.AmbientLifecycleCallback
): AmbientLifecycleObserver

Create a new AmbientLifecycleObserver for use on a real device.

Applications which wish to show layouts in ambient mode should attach the returned observer to their activities or fragments, passing in a set of callback to be notified about ambient state. In addition, the app needs to declare that it uses the android.Manifest.permission.WAKE_LOCK permission in its manifest.

The created AmbientLifecycleObserver can also be used to query whether the device is in ambient mode.

As an example of how to use this class, see the following example:

class MyActivity : ComponentActivity() {
private val callbacks = object : AmbientLifecycleObserver.AmbientLifecycleCallback {
// ...
}

private val ambientObserver = AmbientLifecycleObserver(this, callbacks)

override fun onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
lifecycle.addObserver(ambientObserver)
}
}
Parameters
activity: Activity

The activity that this observer is being attached to.

callbacks: AmbientLifecycleObserver.AmbientLifecycleCallback

An instance of AmbientLifecycleObserver.AmbientLifecycleCallback, used to notify the observer about changes to the ambient state.

AmbientLifecycleObserver

Added in 1.3.0
fun AmbientLifecycleObserver(
    activity: Activity,
    callbackExecutor: Executor,
    callbacks: AmbientLifecycleObserver.AmbientLifecycleCallback
): AmbientLifecycleObserver

Create a new AmbientLifecycleObserver for use on a real device.

Applications which wish to show layouts in ambient mode should attach the returned observer to their activities or fragments, passing in a set of callback to be notified about ambient state. In addition, the app needs to declare that it uses the android.Manifest.permission.WAKE_LOCK permission in its manifest.

The created AmbientLifecycleObserver can also be used to query whether the device is in ambient mode.

As an example of how to use this class, see the following example:

class MyActivity : ComponentActivity() {
private val callbacks = object : AmbientLifecycleObserver.AmbientLifecycleCallback {
// ...
}

private val ambientObserver = AmbientLifecycleObserver(this, mainExecutor, callbacks)

override fun onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
lifecycle.addObserver(ambientObserver)
}
}

If the observer is registered while the device is in ambient mode, the registered callback will immediately receive a call to AmbientLifecycleObserver.AmbientLifecycleCallback.onEnterAmbient. If the device is in active mode, the callbacks will be registered, and onEnterAmbient will be called when the device next enters ambient mode.

Parameters
activity: Activity

The activity that this observer is being attached to.

callbackExecutor: Executor

The executor to run the provided callbacks on.

callbacks: AmbientLifecycleObserver.AmbientLifecycleCallback

An instance of AmbientLifecycleObserver.AmbientLifecycleCallback, used to notify the observer about changes to the ambient state.