67.6 Binding the Client to the Service
For the purposes of this tutorial, the client is the MainActivity instance of the running application. As previously noted, in order to successfully bind to a service and receive the IBinder object returned by the service’s onBind() method, it is necessary to create a ServiceConnection subclass and implement onServiceConnected() and onServiceDisconnected() callback methods. Edit the MainActivity.kt file and modify it as follows:
package com.ebookfrenzy.localbound
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.content.ComponentName
import android.content.Context
import android.content.ServiceConnection
import android.os.IBinder
import android.content.Intent
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
var myService: BoundService? = null
...