AmbientModeSupport

public final class AmbientModeSupport extends Fragment implements LifecycleOwner, ViewModelStoreOwner, HasDefaultViewModelProviderFactory, LifecycleOwner, SavedStateRegistryOwner, ActivityResultCaller

Object
   ↳ Fragment
     ↳ AmbientModeSupport

Use this as a headless Fragment to add ambient support to an Activity on Wearable devices.

The application that uses this should add the WAKE_LOCK permission to its manifest.

The following describes the general use of this class:

Create a subclass of one of the FragmentActivity classes and implement the AmbientCallbackProvider interface. Override the getAmbientCallback method to provide the callbacks required for reacting to the ambient events from the Android system. If a valid AmbientCallback is not provided (either no implementation of the AmbientCallbackProvider interface, or returning null from getAmbientCallback), then ambient mode will NOT be enabled.

The primary entry point for this code is the attach method. It should be called with an FragmentActivity as an argument and that FragmentActivity will then be able to receive ambient lifecycle events through an AmbientCallback. The FragmentActivity will also receive a AmbientController object from the attachment which can be used to query the current status of the ambient mode.

An example of how to implement the AmbientCallbackProvider interface, attach AmbientModeSupport to your FragmentActivity and use the AmbientController can be found below:

public class MyActivity extends FragmentActivity
    implements AmbientModeSupport.AmbientCallbackProvider {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState)
        ...
        AmbientModeSupport.AmbientController controller = AmbientModeSupport.attach(this);
        boolean isAmbient = controller.isAmbient();
    }
    @Override
    AmbientModeSupport.AmbientCallback getAmbientCallback() {
        return new AmbientModeSupport.AmbientCallback() {
            public void onEnterAmbient(Bundle ambientDetails) {...}
            public void onExitAmbient(Bundle ambientDetails) {...}
        }
    }
}

Summary

Nested types

AmbientModeSupport.AmbientCallback

Callback to receive ambient mode state changes.

AmbientModeSupport.AmbientCallbackProvider

Interface for any Activity that wishes to implement Ambient Mode.

AmbientModeSupport.AmbientController

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

Constants

static final String

Property in bundle passed to AmbientCallback#onEnterAmbient(Bundle) to indicate whether burn-in protection is required.

static final String

Property in bundle passed to AmbientCallback#onEnterAmbient(Bundle) to indicate whether the device has low-bit ambient mode.

static final String

Fragment tag used by default when adding AmbientModeSupport to add ambient support to a FragmentActivity.

Public constructors

Constructor

Public methods

static AmbientModeSupport.AmbientController
<T extends FragmentActivity> attach(T activity)

Attach ambient support to the given activity.

@Override void
dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args)

Print the Fragments's state into the given stream.

@Override @CallSuper void
onAttach(Context context)

Called when a fragment is first attached to its context.

@Override @CallSuper void
onCreate(Bundle savedInstanceState)

Called to do initial creation of a fragment.

@Override @CallSuper void

Called when the fragment is no longer in use.

@Override @CallSuper void

Called when the fragment is no longer attached to its activity.

@Override @CallSuper void

Called when the Fragment is no longer resumed.

@Override @CallSuper void

Called when the fragment is visible to the user and actively running.

@Override @CallSuper void

Called when the Fragment is no longer started.

Inherited methods

From class ActivityResultCaller
abstract @NonNull ActivityResultLauncher<I>
<I, O> registerForActivityResult(
    @NonNull ActivityResultContract<I, O> contract,
    @NonNull ActivityResultCallback<O> callback
)

Register a request to start an activity for result, designated by the given contract.

abstract @NonNull ActivityResultLauncher<I>
<I, O> registerForActivityResult(
    @NonNull ActivityResultContract<I, O> contract,
    @NonNull ActivityResultRegistry registry,
    @NonNull ActivityResultCallback<O> callback
)

Register a request to start an activity for result, designated by the given contract.

From class ComponentCallbacks
abstract void