-
Notifications
You must be signed in to change notification settings - Fork 12
Change to avoid recreating notification and ongoing activity #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
| */ | ||
| package com.android.example.wear.ongoingactivity | ||
|
|
||
| import android.Manifest | ||
| import android.app.Notification | ||
| import android.app.NotificationChannel | ||
| import android.app.NotificationManager | ||
|
|
@@ -27,9 +28,11 @@ import android.content.res.Configuration | |
| import android.os.Binder | ||
| import android.os.IBinder | ||
| import android.util.Log | ||
| import androidx.annotation.RequiresPermission | ||
| import androidx.core.app.NotificationCompat | ||
| import androidx.lifecycle.LifecycleService | ||
| import androidx.lifecycle.lifecycleScope | ||
| import androidx.wear.ongoing.OngoingActivity | ||
| import com.android.example.wear.ongoingactivity.data.WalkingWorkoutsRepository | ||
| import kotlinx.coroutines.Job | ||
| import kotlinx.coroutines.delay | ||
|
|
@@ -63,6 +66,8 @@ class ForegroundOnlyWalkingWorkoutService : LifecycleService() { | |
| } | ||
|
|
||
| private lateinit var notificationManager: NotificationManager | ||
| private lateinit var notification: Notification | ||
| private lateinit var ongoingActivity: OngoingActivity | ||
|
|
||
| /* | ||
| * Checks whether the bound activity has really gone away (in which case a foreground service | ||
|
|
@@ -141,9 +146,9 @@ class ForegroundOnlyWalkingWorkoutService : LifecycleService() { | |
| // NOTE: If this method is called due to a configuration change in MainActivity, | ||
| // we do nothing. | ||
| if (!configurationChange && walkingWorkoutActive) { | ||
| Log.d(TAG, "Start foreground service") | ||
| val notification = | ||
| notification = | ||
| generateNotification(getString(R.string.walking_workout_notification_started_text)) | ||
| updateOngoingActivity(getString(R.string.walking_workout_notification_started_text)) | ||
| // startForeground takes care of notificationManager.notify(...). | ||
| startForeground(NOTIFICATION_ID, notification, FOREGROUND_SERVICE_TYPE_DATA_SYNC) | ||
| serviceRunningInForeground = true | ||
|
|
@@ -164,6 +169,7 @@ class ForegroundOnlyWalkingWorkoutService : LifecycleService() { | |
| configurationChange = false | ||
| } | ||
|
|
||
| @RequiresPermission(Manifest.permission.POST_NOTIFICATIONS) | ||
| fun startWalkingWorkout() { | ||
| Log.d(TAG, "startWalkingWorkout()") | ||
|
|
||
|
|
@@ -210,13 +216,12 @@ class ForegroundOnlyWalkingWorkoutService : LifecycleService() { | |
| // Normally, you would listen to the location and sensor data and calculate your points with | ||
| // an algorithm, but we are mocking the data to simply this so we can focus on learning about | ||
| // the Ongoing Activity API. | ||
| @RequiresPermission(Manifest.permission.POST_NOTIFICATIONS) | ||
| private suspend fun mockSensorAndLocationForWalkingWorkout() { | ||
| for (walkingPoints in 0 until 100) { | ||
| if (serviceRunningInForeground) { | ||
| val notification = generateNotification( | ||
| getString(R.string.walking_points_text, walkingPoints), | ||
| ) | ||
| notificationManager.notify(NOTIFICATION_ID, notification) | ||
| val updatedStatus = getString(R.string.walking_points_text, walkingPoints) | ||
| updateOngoingActivity(updatedStatus) | ||
|
Comment on lines
+223
to
+224
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change replaces the notification update logic with a call to |
||
| } | ||
| Log.d(TAG, "mockSensorAndLocationForWalkingWorkout(): $walkingPoints") | ||
| walkingWorkoutsRepository.setWalkingPoints(walkingPoints) | ||
|
|
@@ -311,6 +316,11 @@ class ForegroundOnlyWalkingWorkoutService : LifecycleService() { | |
| return notificationBuilder.build() | ||
| } | ||
|
|
||
| @RequiresPermission(Manifest.permission.POST_NOTIFICATIONS) | ||
| private fun updateOngoingActivity(statusText: String) { | ||
| // TODO: Update the Ongoing Activity. | ||
| } | ||
|
|
||
| /** | ||
| * Class used for the client Binder. Since this service runs in the same process as its | ||
| * clients, we don't need to deal with IPC. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This log statement, which is useful for debugging, was removed. However, it's kept in the
finishedversion of the file. This seems like an accidental deletion and introduces an inconsistency between thestartandfinishedstates of the codelab. Please consider keeping it.