Skip to content

Commit 43ac10f

Browse files
Add snippets for /training/wearables/always-on (#691)
1 parent 36730f7 commit 43ac10f

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.wear.snippets.alwayson
18+
19+
import android.os.Bundle
20+
import androidx.activity.ComponentActivity
21+
import androidx.wear.ambient.AmbientLifecycleObserver
22+
23+
// [START android_wear_ongoing_activity_ambientlifecycleobserver]
24+
val ambientCallback = object : AmbientLifecycleObserver.AmbientLifecycleCallback {
25+
override fun onEnterAmbient(ambientDetails: AmbientLifecycleObserver.AmbientDetails) {
26+
// ... Called when moving from interactive mode into ambient mode.
27+
// Adjust UI for low-power state: dim colors, hide non-essential elements.
28+
}
29+
30+
override fun onExitAmbient() {
31+
// ... Called when leaving ambient mode, back into interactive mode.
32+
// Restore full UI.
33+
}
34+
35+
override fun onUpdateAmbient() {
36+
// ... Called by the system periodically (typically once per minute)
37+
// to allow the app to update its display while in ambient mode.
38+
}
39+
}
40+
// [END android_wear_ongoing_activity_ambientlifecycleobserver]
41+
42+
class AmbientLifecycleActivity : ComponentActivity() {
43+
44+
private val activity = this // rename so the snippet reads better
45+
// [START android_wear_ongoing_activity_ambientlifecycleobserver_oncreate]
46+
private val ambientObserver = AmbientLifecycleObserver(activity, ambientCallback)
47+
48+
override fun onCreate(savedInstanceState: Bundle?) {
49+
super.onCreate(savedInstanceState)
50+
lifecycle.addObserver(ambientObserver)
51+
52+
// ...
53+
}
54+
// [END android_wear_ongoing_activity_ambientlifecycleobserver_oncreate]
55+
56+
// [START android_wear_ongoing_activity_ambientlifecycleobserver_ondestroy]
57+
override fun onDestroy() {
58+
super.onDestroy()
59+
lifecycle.removeObserver(ambientObserver)
60+
61+
// ...
62+
}
63+
// [END android_wear_ongoing_activity_ambientlifecycleobserver_ondestroy]
64+
}

0 commit comments

Comments
 (0)