Junior — Middle
What types of services have you used in your projects?
sobes.tech AI
Answer from AI
In Android development, I used the following types of services:
-
Foreground Service — a service that runs in the foreground and displays a notification. It is used for tasks that should continue even when the app is minimized, such as music playback or location tracking.
-
Background Service — a service that runs in the background without a user interface. It is typically used for long-running operations like data downloads or synchronization.
-
Bound Service — a service that other components can bind to for interaction and data exchange. It is often used to implement client-server logic within the app.
Example of creating a Foreground Service:
class MusicService : Service() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
val notification = createNotification() // create notification
startForeground(1, notification) // start service in the foreground
// music playback logic
return START_STICKY
}
override fun onBind(intent: Intent?): IBinder? = null
}