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

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