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 it

Example:

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 CaseWhy Bound Service?
Music player controlsActivity can call play/pause
Bluetooth device controlActivity can send commands
TimerActivity can read current time
Sensor dataActivity can get live sensor values
Counter serviceActivity 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 methods


5. 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

BenefitExplanation
Two-way communicationActivity can call service methods
Good for live controlUseful for music/Bluetooth/timer
Shares current stateActivity can get service data
FlexibleCan be combined with Started Service


7. Drawbacks of Bound Service

DrawbackExplanation
More complexNeeds ServiceConnection
Must unbind properlyForgetting unbind can cause leaks
Usually depends on clientStops when all clients unbind
Not ideal for independent long workStarted/Foreground Service may be better