As mentioned earlier, there are two ways to start a Service: startService and bindService. The former has already said how to use it. Therefore, this post is mainly about bind service. Here we will discuss only those services that are bound, and those services that are both startService () and bindService () are not in this scope.
① The Bind Service is like a server in the C/S architecture. Other components (such as Activity) are bound to it (through bindService () and can send requests to it, it can accept the response returned from it, and even provides the inter-process communication (IPC) function.
② To bind a service to another component, its onBind () method must be implemented and an IBinder object must be returned, other components can then communicate with the service through the IBinder object.
③ Multiple clients can be bound to the same service, but the onBind () method of the service is called only when the first client is bound to it, when another client is bound to it again, it does not call the onBind () method, but directly returns the IBinder object generated when it is called for the first time. In other words, onBind () is called only once in its lifecycle.
④ Shows the lifecycle of the Bind Service:
⑤ The Bind Service will not run permanently in the background, but when all components bound to it call unbindService () for unbinding, the system will stop it to recycle resources.
⑥ When we want to implement a Bind Service, the most important thing is to implement its onBind () method to return an IBinder object.
To be updated