What does the Binder class do? What is the meaning of binding in Android bound services?

Bound service:

A bound service is one that allows application components to bind to it by calling bindService() to create a long-standing connection.

Create a bound service when you want to interact with the service from activities and other components in your application or to expose some of your application's functionality to other applications through interprocess communication (IPC).

To create a bound service, implement the onBind() callback method to return an IBinder that defines the interface for communication with the service. Other application components can then call bindService() to retrieve the interface and begin calling methods on the service. The service lives only to serve the application component that is bound to it, so when there are no components bound to the service, the system destroys it. You do not need to stop a bound service in the same way that you must when the service is started through onStartCommand().

IBinder:

To create a bound service, you must define the interface that specifies how a client can communicate with the service. This interface between the service and a client must be an implementation of IBinder and is what your service must return from the onBind() callback method. After the client receives the IBinder, it can begin interacting with the service through that interface.

onBind():

The system invokes this method by calling bindService() when another component wants to bind with the service (such as to perform RPC). In your implementation of this method, you must provide an interface that clients use to communicate with the service by returning an IBinder. You must always implement this method; however, if you don't want to allow binding, you should return null.


this is an example works as completion to the answer above

  1. inside your service class, initialize the IBinder interface with the object created by our inner class (check step 2)
  2. create an inner class extends Binder that has a getter function, to gain access to the service class
  3. in your service class ovveride onBind function, and use it to return the instance we created in step 1

**The code will clear it for you **

public class MyServiceClass extends Service {

//binder given to client  
private final IBinder mBinder = new LocalBinder();

//our inner class 
public LocalBinder extends Binder {
public MyServiceClass getService() {
        return MyServiceClass.this;
    }
}
@Override
public IBinder onBind(Intent intent) {
    return mBinder;
}

public void doSomeWork(int time){ //we will call this function from outside, which is the whole idea of this **Binding**}

  }

Next step is binding itself

in your MainClass or whatever where you want to bind your service,

  1. Defines callbacks for service binding, passed to bindService()

    private ServiceConnection serviceConnection = new ServiceConnection(){

    @Override
    public void onServiceConnected(ComponentName className, IBinder service) {
       MyServiceClass.LocalBinder binder =(MyServiceClass.LocalBinder)service;
       timerService = binder.getService();
    }
    
    @Override
    public void onServiceDisconnected(ComponentName arg0) {
        //what to do if service diconnect
    }
    

    };

  2. the moment of binding

    Intent intent = new Intent(this, MyServiceClass.class);
    bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
    

to unbind the service

unbindService(serviceConnection);
  1. then you call the public function we created before in the Service class using the help of [timerService = binder.getService();]
    timerService.doSomeWork(50);