59.6 Launching and Binding to the Remote Service
As with a local bound service, the client component needs to implement an instance of the ServiceConnection class with onServiceConnected() and onServiceDisconnected() methods. Also, in common with local services, the onServiceConnected() method will be passed the IBinder object returned by the onBind() method of the remote service which will be used to send messages to the server handler. In the case of this example, the client is MainActivity, the code for which is located in MainActivity.java. Load this file and modify it to add the ServiceConnection class and a variable to store a reference to the received Messenger object together with a Boolean flag to indicate whether or not the connection is established:
package com.ebookfrenzy.remotebound;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
...