@@ -19,7 +19,6 @@ package com.example.wear.snippets.alwayson
1919import android.app.NotificationChannel
2020import android.app.NotificationManager
2121import android.app.PendingIntent
22- import android.content.Context
2322import android.content.Intent
2423import android.os.SystemClock
2524import android.util.Log
@@ -30,29 +29,19 @@ import androidx.wear.ongoing.OngoingActivity
3029import androidx.wear.ongoing.Status
3130import com.example.wear.R
3231
33- class AlwaysOnService : LifecycleService () {
32+ abstract class AlwaysOnServiceBase : LifecycleService () {
3433
3534 private val notificationManager by lazy { getSystemService<NotificationManager >() }
3635
3736 companion object {
3837 private const val TAG = " AlwaysOnService"
39- private const val NOTIFICATION_ID = 1001
40- private const val CHANNEL_ID = " always_on_service_channel"
38+ const val NOTIFICATION_ID = 1001
39+ const val CHANNEL_ID = " always_on_service_channel"
4140 private const val CHANNEL_NAME = " Always On Service"
41+
4242 @Volatile
4343 var isRunning = false
4444 private set
45-
46- fun startService (context : Context ) {
47- Log .d(TAG , " Starting AlwaysOnService" )
48- val intent = Intent (context, AlwaysOnService ::class .java)
49- context.startForegroundService(intent)
50- }
51-
52- fun stopService (context : Context ) {
53- Log .d(TAG , " Stopping AlwaysOnService" )
54- context.stopService(Intent (context, AlwaysOnService ::class .java))
55- }
5645 }
5746
5847 override fun onCreate () {
@@ -66,16 +55,15 @@ class AlwaysOnService : LifecycleService() {
6655 super .onStartCommand(intent, flags, startId)
6756 Log .d(TAG , " onStartCommand: Service started with startId: $startId " )
6857
69- // Switch between different types of ongoing notification
70- createNotification1()
71- // createNotification2()
72- // createNotification3()
58+ createNotification()
7359
7460 Log .d(TAG , " onStartCommand: Service is now running as foreground service" )
7561
7662 return START_STICKY
7763 }
7864
65+
66+
7967 override fun onDestroy () {
8068 Log .d(TAG , " onDestroy: Service destroyed" )
8169 isRunning = false
@@ -94,8 +82,12 @@ class AlwaysOnService : LifecycleService() {
9482 Log .d(TAG , " createNotificationChannel: Notification channel created" )
9583 }
9684
97- // Creates an ongoing activity that demonstrates how to link the touch intent to the always-on activity.
98- private fun createNotification1 () {
85+ abstract fun createNotification ()
86+ }
87+
88+ class AlwaysOnService1 : AlwaysOnServiceBase () {
89+ override fun createNotification () {
90+ // Creates an ongoing activity that demonstrates how to link the touch intent to the always-on activity.
9991 // [START android_wear_ongoing_activity_create_notification]
10092 val activityIntent =
10193 Intent (this , AlwaysOnActivity ::class .java).apply {
@@ -145,9 +137,11 @@ class AlwaysOnService : LifecycleService() {
145137
146138 startForeground(NOTIFICATION_ID , notification)
147139 }
140+ }
148141
149- // Creates an ongoing activity with a static status text
150- private fun createNotification2 () {
142+ class AlwaysOnService2 : AlwaysOnServiceBase () {
143+ override fun createNotification () {
144+ // Creates an ongoing activity with a static status text
151145
152146 // [START android_wear_ongoing_activity_notification_builder]
153147 // Create a PendingIntent to pass to the notification builder
@@ -192,9 +186,11 @@ class AlwaysOnService : LifecycleService() {
192186 startForeground(NOTIFICATION_ID , notificationBuilder.build())
193187 // [END android_wear_ongoing_activity_post_notification]
194188 }
189+ }
195190
196- // Creates an ongoing activity that demonstrates dynamic status text (a timer)
197- private fun createNotification3 () {
191+ class AlwaysOnService3 : AlwaysOnServiceBase () {
192+ override fun createNotification () {
193+ // Creates an ongoing activity that demonstrates dynamic status text (a timer)
198194 val activityIntent =
199195 Intent (this , AlwaysOnActivity ::class .java).apply {
200196 flags = Intent .FLAG_ACTIVITY_SINGLE_TOP
@@ -252,4 +248,4 @@ class AlwaysOnService : LifecycleService() {
252248 ongoingActivity.apply (applicationContext)
253249 startForeground(NOTIFICATION_ID , notificationBuilder.build())
254250 }
255- }
251+ }
0 commit comments