I’ll explain Android Services like a tutorial: first meaning and lifecycle, then Started/Foreground/Bound services, pseudocode, Kotlin examples, and scenario-based interview cases.
Common Mistakes
Mistake 1: Doing Heavy Work on Main Thread
Wrong Code
override fun onStartCommand(
intent: Intent?,
flags: Int,
startId: Int
): Int {
uploadLargeFile()
return START_NOT_STICKY
}Correct Code
override fun onStartCommand(
intent: Intent?,
flags: Int,
startId: Int
): Int {
Thread {
uploadLargeFile()
stopSelf(startId)
}.start()
return START_NOT_STICKY
}Mistake 2: Starting Background Service from Background App
Modern Android restricts background execution.
Better Approach
Use WorkManager for deferrable background tasks.
Use Foreground Service only for visible urgent tasks.Mistake 3: Foreground Service Without Notification
Wrong
Start foreground service without showing notificationCorrect
Create notification
Call startForeground()
Then start actual workMistake 4: Forgetting to Stop Service
Wrong
Upload completed but service is still runningCorrect
stopSelf()Mistake 5: Forgetting to Unbind Bound Service
Wrong
Activity destroyed but service is still connectedCorrect
if (isBound) {
unbindService(connection)
isBound = false
}Interview Questions and Answers
Q1. What is a Service?
A Service is an Android component used to perform work without a UI.
It can be:
Started Service
Foreground Service
Bound ServiceQ2. Does Service run on background thread?
No.
A Service runs on the main thread by default.
For heavy work, use:Thread
Coroutine
Executor
WorkManagerQ3. What is Foreground Service?
A Foreground Service performs user-visible ongoing work and must show a notification.
Examples:
Music
Navigation
Fitness tracking
Ongoing call
Location trackingQ4. What is Bound Service?
A Bound Service allows Activity or another component to connect with Service and call its methods.
Example:
Activity controls music service using play/pause methods.Q5. Difference Between Started and Bound Service
| Point | Started Service | Bound Service |
|---|---|---|
| Started by | startService() | bindService() |
| Communication | One-way | Two-way |
| Activity calls methods? | No | Yes |
| Stops when | stopSelf() / stopService() | All clients unbind |
| Example | Upload file | Music controller |
Q6. What happens if Service is killed?
It depends on onStartCommand() return value.
| Return Type | Result |
|---|---|
START_NOT_STICKY | Do not restart |
START_STICKY | Restart, but intent may be null |
START_REDELIVER_INTENT | Restart with old intent |
Q7. Should we use Service for periodic background sync?
Usually no.
Use:
WorkManagerbecause it handles:
Battery optimization
Network constraint
Retry
Background restrictionsQ8. Can one Service be both Started and Bound?
Yes.
Example:
Music Service
startService()
-> Keeps music running
bindService()
-> Allows Activity to control musicFinal Memory Trick
Started Service
= Do this work
Foreground Service
= Do this work and show notification
Bound Service
= Let me connect and talk to you
WorkManager
= Do this work later/reliablyFinal Short Summary
| Concept | Meaning |
|---|---|
| Android Service | Work without UI |
| Started Service | One-way independent work |
| Foreground Service | User-visible long-running work |
| Bound Service | Two-way communication with Activity |
| WorkManager | Reliable deferrable background work |
Best Practice
Use Foreground Service only for active user-visible tasks.
Use WorkManager for deferrable background tasks.
Use Bound Service when Activity needs direct communication.
Never do heavy work directly on main thread inside Service.
Always stop or unbind Service properly.