What does the Binder class do? What does binding mean in linked Android services?

I am completely confused with related services. My questions:

  • What's the point of tying?
  • What does the Binder class do?
  • What does "return an IBinder to interact with a service" mean?
  • What is an IBinder object?
  • How does the onBind () method work?

These are questions about related services. Please explain in detail. I've already read the documentation, but it's still unclear to me.

+7


source to share


1 answer


Related 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 linked service if you want to interact with the service from actions and other components of your application, or to expose some of your application's functionality to other applications through interprocess communication (IPC).

To create a bound service, use the onBind () callback method to return an IBinder that defines the interface to communicate with the service. Other components of the application can then call bindService () to retrieve the interface and start calling methods on the service. The service only works to serve the application component that is bound to it, so when there are no components associated with the service, the system destroys it. You don't need to stop the associated service as much as you should when the service is started via onStartCommand ().



IBinder:

To create a linked service, you must define an interface that defines how the client interacts with the service. This interface between the service and the client should be an implementation of IBinder and is what your service should return from the onBind () callback method. After the client receives the IBinder, he can start interacting with the service through this interface.

onBind ():

The system calls this method by calling bindService () when another component wants to communicate with the service (for example, to perform an RPC). When you implement this method, you must provide the interface that clients use to communicate with the service, returning an IBinder. You should always implement this method; however, if you don't want to allow binding, you must return null.

+8


source







All Articles