Bound Service
1. What is a Bound Service?
A Bound Service allows another component, like Activity or Fragment, to connect with the service and call its methods.
Simple Meaning
Bound Service = Activity connects to Service and talks to itExample:
Activity connects to MusicService
Activity calls play()
Activity calls pause()
Activity checks isPlaying()2. When Should We Use Bound Service?
Use Bound Service when Activity needs direct communication with Service.
Good Examples
| Use Case | Why Bound Service? |
|---|---|
| Music player controls | Activity can call play/pause |
| Bluetooth device control | Activity can send commands |
| Timer | Activity can read current time |
| Sensor data | Activity can get live sensor values |
| Counter service | Activity can increment/read counter |
3. Bound Service Lifecycle
bindService()
↓
onCreate()
↓
onBind()
↓
Service Bound
↓
Activity calls service methods
↓
unbindService()
↓
onUnbind()
↓
onDestroy()4. How Bound Service Works Internally
Activity
↓ // bindService()
Android System
↓
Service.onCreate()
↓
Service.onBind()
↓ // returns Binder object
Activity receives Binder
↓
Activity gets Service object
↓
Activity calls Service methods5. Bound Service Code Example
BoundMusicService.kt
class BoundMusicService : Service() {
private val binder = MusicBinder()
inner class MusicBinder : Binder() {
fun getService(): BoundMusicService {
return this@BoundMusicService
}
}
override fun onBind(intent: Intent?): IBinder {
return binder
}
fun playMusic() {
Log.d("BoundMusicService", "Playing music")
}
fun pauseMusic() {
Log.d("BoundMusicService", "Music paused")
}
fun isPlaying(): Boolean {
return true
}
}AndroidManifest.xml
<service
android:name=".BoundMusicService"
android:exported="false" />MainActivity.kt
class MainActivity : AppCompatActivity() {
private var musicService: BoundMusicService? = null
private var isBound = false
private val connection = object : ServiceConnection {
override fun onServiceConnected(
name: ComponentName?,
service: IBinder?
) {
val binder = service as BoundMusicService.MusicBinder
musicService = binder.getService()
isBound = true
musicService?.playMusic()
}
override fun onServiceDisconnected(name: ComponentName?) {
musicService = null
isBound = false
}
}
override fun onStart() {
super.onStart()
val intent = Intent(this, BoundMusicService::class.java)
bindService(intent, connection, Context.BIND_AUTO_CREATE)
}
override fun onStop() {
super.onStop()
if (isBound) {
unbindService(connection)
isBound = false
}
}
}6. Benefits of Bound Service
| Benefit | Explanation |
|---|---|
| Two-way communication | Activity can call service methods |
| Good for live control | Useful for music/Bluetooth/timer |
| Shares current state | Activity can get service data |
| Flexible | Can be combined with Started Service |
7. Drawbacks of Bound Service
| Drawback | Explanation |
|---|---|
| More complex | Needs ServiceConnection |
| Must unbind properly | Forgetting unbind can cause leaks |
| Usually depends on client | Stops when all clients unbind |
| Not ideal for independent long work | Started/Foreground Service may be better |