58.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.java file and modify it as follows:
package com.ebookfrenzy.localbound;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.IBinder;
import android.content.Context;
import android.content.Intent;
import android.content.ComponentName;
import android.content.ServiceConnection;
import com.ebookfrenzy.localbound.BoundService.MyLocalBinder;
public class MainActivity extends AppCompatActivity {
BoundService myService;
...