Skip to content

Commit f2d56bc

Browse files
Ongoing activity improvements
1 parent 1c6cdf2 commit f2d56bc

File tree

1 file changed

+105
-13
lines changed

1 file changed

+105
-13
lines changed

wear/src/main/java/com/example/wear/snippets/alwayson/AlwaysOnService.kt

Lines changed: 105 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,7 @@ class AlwaysOnService : LifecycleService() {
6868
super.onStartCommand(intent, flags, startId)
6969
Log.d(TAG, "onStartCommand: Service started with startId: $startId")
7070

71-
// Create and start foreground notification
72-
val notification = createNotification()
73-
startForeground(NOTIFICATION_ID, notification)
71+
createNotification1()
7472

7573
Log.d(TAG, "onStartCommand: Service is now running as foreground service")
7674

@@ -95,8 +93,9 @@ class AlwaysOnService : LifecycleService() {
9593
Log.d(TAG, "createNotificationChannel: Notification channel created")
9694
}
9795

98-
// [START android_wear_ongoing_activity_create_notification]
99-
private fun createNotification(): Notification {
96+
// Creates an ongoing activity that demonstrates how to link the touch intent to the always-on activity.
97+
private fun createNotification1(): Unit {
98+
// [START android_wear_ongoing_activity_create_notification]
10099
val activityIntent =
101100
Intent(this, AlwaysOnActivity::class.java).apply {
102101
flags = Intent.FLAG_ACTIVITY_SINGLE_TOP
@@ -124,7 +123,7 @@ class AlwaysOnService : LifecycleService() {
124123
.setOngoing(true)
125124

126125
// [START_EXCLUDE]
127-
val ongoingActivityStatus = createOngoingStatus()
126+
val ongoingActivityStatus = Status.Builder().addTemplate("Stopwatch running").build()
128127
// [END_EXCLUDE]
129128

130129
val ongoingActivity =
@@ -140,25 +139,118 @@ class AlwaysOnService : LifecycleService() {
140139

141140
ongoingActivity.apply(applicationContext)
142141

143-
return notificationBuilder.build()
142+
val notification = notificationBuilder.build()
143+
// [END android_wear_ongoing_activity_create_notification] // where's the equivalent START? should this even be here? check the docs jjjjjjj
144+
145+
startForeground(NOTIFICATION_ID, notification)
144146
}
145-
// [END android_wear_ongoing_activity_create_notification]
146147

147-
private fun createOngoingStatus(): Status {
148+
// Creates an ongoing activity with a static status text
149+
private fun createNotification2(): Unit {
150+
151+
// [START android_wear_ongoing_activity_notification_builder]
152+
// Create a PendingIntent to pass to the notification builder
153+
val pendingIntent =
154+
PendingIntent.getActivity(
155+
this,
156+
0,
157+
Intent(this, AlwaysOnActivity::class.java).apply {
158+
flags = Intent.FLAG_ACTIVITY_SINGLE_TOP
159+
},
160+
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE,
161+
)
162+
163+
val notificationBuilder = NotificationCompat.Builder(this, CHANNEL_ID)
164+
.setContentTitle("Always On Service")
165+
.setContentText("Service is running in background")
166+
.setSmallIcon(R.drawable.animated_walk)
167+
// Category helps the system prioritize the ongoing activity
168+
.setCategory(NotificationCompat.CATEGORY_WORKOUT)
169+
.setContentIntent(pendingIntent)
170+
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
171+
.setOngoing(true) // Important!
172+
// [END android_wear_ongoing_activity_notification_builder]
173+
174+
// [START android_wear_ongoing_activity_builder]
175+
val ongoingActivity =
176+
OngoingActivity.Builder(applicationContext, NOTIFICATION_ID, notificationBuilder)
177+
// Sets the icon that appears on the watch face in active mode.
178+
.setAnimatedIcon(R.drawable.animated_walk)
179+
// Sets the icon that appears on the watch face in ambient mode.
180+
.setStaticIcon(R.drawable.ic_walk)
181+
// Sets the tap target to bring the user back to the app.
182+
.setTouchIntent(pendingIntent)
183+
.build()
184+
// [END android_wear_ongoing_activity_builder]
185+
186+
// [START android_wear_ongoing_activity_post_notification]
187+
// This call modifies notificationBuilder to include the ongoing activity data.
188+
ongoingActivity.apply(applicationContext)
189+
190+
// Post the notification.
191+
startForeground(NOTIFICATION_ID, notificationBuilder.build())
192+
// [END android_wear_ongoing_activity_post_notification]
193+
}
194+
195+
// Creates an ongoing activity that demonstrates dynamic status text (a timer)
196+
private fun createNotification3(): Unit {
197+
val activityIntent =
198+
Intent(this, AlwaysOnActivity::class.java).apply {
199+
flags = Intent.FLAG_ACTIVITY_SINGLE_TOP
200+
}
201+
202+
val pendingIntent =
203+
PendingIntent.getActivity(
204+
this,
205+
0,
206+
activityIntent,
207+
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE,
208+
)
209+
210+
val notificationBuilder = NotificationCompat.Builder(this, CHANNEL_ID)
211+
.setContentTitle("Always On Service")
212+
.setContentText("Service is running in background")
213+
.setSmallIcon(R.drawable.animated_walk)
214+
// Category helps the system prioritize the ongoing activity
215+
.setCategory(NotificationCompat.CATEGORY_WORKOUT)
216+
.setContentIntent(pendingIntent)
217+
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
218+
.setOngoing(true) // Important!
219+
148220
// [START android_wear_ongoing_activity_create_status]
221+
// Define a template with placeholders for the activity type and the timer.
149222
val statusTemplate = "#type# for #time#"
150223

151-
// Creates a 5 minute timer.
152-
// Note the use of SystemClock.elapsedRealtime(), not System.currentTimeMillis().
153-
val runStartTime = SystemClock.elapsedRealtime() + TimeUnit.MINUTES.toMillis(5)
224+
// Set the start time for a stopwatch.
225+
// Use SystemClock.elapsedRealtime() for time-based parts.
226+
val runStartTime = SystemClock.elapsedRealtime()
154227

155228
val ongoingActivityStatus = Status.Builder()
229+
// Sets the template string.
156230
.addTemplate(statusTemplate)
231+
// Fills the #type# placeholder with a static text part.
157232
.addPart("type", Status.TextPart("Run"))
233+
// Fills the #time# placeholder with a stopwatch part.
158234
.addPart("time", Status.StopwatchPart(runStartTime))
159235
.build()
160236
// [END android_wear_ongoing_activity_create_status]
161237

162-
return ongoingActivityStatus
238+
// [START android_wear_ongoing_activity_set_status]
239+
val ongoingActivity =
240+
OngoingActivity.Builder(applicationContext, NOTIFICATION_ID, notificationBuilder)
241+
// [START_EXCLUDE]
242+
.setAnimatedIcon(R.drawable.animated_walk)
243+
.setStaticIcon(R.drawable.ic_walk)
244+
.setTouchIntent(pendingIntent)
245+
// [END_EXCLUDE]
246+
// Add the status to the OngoingActivity.
247+
.setStatus(ongoingActivityStatus)
248+
.build()
249+
// [END android_wear_ongoing_activity_set_status]
250+
251+
ongoingActivity.apply(applicationContext)
252+
startForeground(NOTIFICATION_ID, notificationBuilder.build())
253+
163254
}
255+
164256
}

0 commit comments

Comments
 (0)