From the course: Concurrent Programming with Android: Threads, Workers, and Kotlin Coroutines
Unlock this course with a free trial
Join today to access over 22,600 courses taught by industry experts.
Run a service in the foreground
From the course: Concurrent Programming with Android: Threads, Workers, and Kotlin Coroutines
Run a service in the foreground
- [Instructor] A service can either be in the foreground or in the background. By default, it's in the background, but you can put it into the foreground by adding a visible notification. As long as the services in the foreground, the notification can't be dismissed by the user. When you take the service out of the foreground, you can close the notification yourself, or you can leave it open so then it can be dismissed by the user. The most common example of using a foreground service is playing music. And that's what I'll demonstrate here. To display a notification on Android eight and later, you need to define a notification channel. So first, I'll add a new function that I'll call create notification channel. And this is going to return a string. I need a couple of string values. The first is called channel ID and I'll set it to a value of my_service, the user won't see that. And then a channel name, which will…
Contents
-
-
-
-
-
-
-
(Locked)
Create and run a bound service6m 20s
-
(Locked)
Started services and background processing limits5m 24s
-
(Locked)
Play audio in a service with MediaPlayer5m 16s
-
(Locked)
Run a service in the foreground9m 45s
-
(Locked)
Send intents from a foreground notification5m 7s
-
(Locked)
Style a foreground notification2m 42s
-
(Locked)
-