A bound service is the server in a client-server interface. It allows components (such as activities) to bind to the service, send requests, receive responses, and perform interprocess communication (IPC).
A bound service typically lives only while it serves another application component and does not run in the background indefinitely.
Before Going further detail of Bound Services. Let Start with a basic Question
“What is Service in android and its types.”
As per Android developer site, Service is one of the basic application component and Its duty is to perform long-running operations in the background. Service has no UI.
Type of services in Android
1. Scheduled
2. Started
3. Bound
Implementation Steps
Using a Messenger
Using AIDL
Messenger is actually based on AIDL as its underlying structure.
The main Difference is :-
Messenger creates a queue of all the client requests in a single thread, so the service receives requests one at a time
But AIDL handle multiple requests simultaneously
A bound service typically lives only while it serves another application component and does not run in the background indefinitely.
Before Going further detail of Bound Services. Let Start with a basic Question
“What is Service in android and its types.”
As per Android developer site, Service is one of the basic application component and Its duty is to perform long-running operations in the background. Service has no UI.
Type of services in Android
1. Scheduled
- JobScheduler API(introduced in Android 5.0 API level 21) used to schedule a service for execution at the appropriate times.
- We can use the JobScheduler by registering jobs and specifying their requirements for network and timing.
- The system then gracefully schedules the jobs
2. Started
- A service is started when any application component (activity, Broadcast etc.) calls startService() method.
- Started service can run in the background indefinitely, even if the component that started it is destroyed.
- Generally a started service performs a single operation and does not return a result to the caller.
3. Bound
- A service is bound when any application component binds to it by calling bindService() method
- A bound service offers a client-server interface that allows components to interact with the service
- A bound service runs only as long as another application component is bound to it
- Multiple components can bind to the service at once, but when all of them unbind, the service is destroyed
- A service runs in the main thread of its hosting process.
- The service by default does not create its own thread and does not run in a separate process unless you specify it.
- For CPU-intensive work(MP3 playback or network related work), you should create a new thread within the service to complete that work.
- By using a separate thread, you can reduce the risk of Application Not Responding (ANR) errors.
Implementation Steps
- A bound service is an implementation of the Service class that allows other applications to bind to it and interact with it.
- To provide binding for a service, you must implement the onBind() callback method.
- clients use IBinder object (return by onBind() method) to interact with the service.
- A client binds to a service by calling bindService().
- Client must provide an implementation of ServiceConnection, which monitors the connection with the service.
- The bindService() method returns immediately without a value
- Android system creates the connection between the client and service and it calls onServiceConnected() on the ServiceConnection, to deliver the IBinder that the client can use to communicate with the service.
- Multiple clients can call bindService() simultaneously. But Android system calls our service's onBind() method to retrieve the IBinder only once when the first client binds.
- After that Android system delivers the same IBinder to any additional clients that bind, without calling onBind() again.
- When the last client unbinds from the service, the system destroys the service, unless the service was also started by startService().
Creating a bound service
- For creating a bound service We must have to provide an IBinder that provides the programming interface
- clients use this interface to interact with the service.
- There are three ways you can define the interface:
- Extending the Binder class
- Using a Messenger
- Using AIDL
- If our service is private to our own application and runs in the same process as the client
- In that case we create our interface by extending the Binder class and returning an instance of it from onBind()
- The client receives the Binder and can use it to directly access public methods available in either the Binder implementation or the Service.
This is the preferred technique when our service is merely a background worker for our own application.
- If we need our interface to work across different processes
- The service defines a Handler that responds to different types of Message objects.
- This Handler is the basis for a Messenger that can then share an IBinder with the client, allowing the client to send commands to the service using Message objects.
- The client can define a Messenger of its own, so the service can send messages back
This is the simplest way to perform interprocess communication (IPC), because the Messenger queues all requests into a single thread so that We don't have to design our service to be thread-safe.
- Android Interface Definition Language (AIDL) decomposes objects into primitives that the operating system can understand and marshals them across processes to perform IPC.
- For using AIDL our service must be thread-safe and capable of multi-threading.
Messenger is actually based on AIDL as its underlying structure.
The main Difference is :-
Messenger creates a queue of all the client requests in a single thread, so the service receives requests one at a time
But AIDL handle multiple requests simultaneously
For more detail on AIDL check my previous blog on AIDL.
Example Code of Binder.....(Coming soon...)
Example Code of Messenger.....(coming soon..)
Example Code of Binder.....(Coming soon...)
Example Code of Messenger.....(coming soon..)
0 komentar:
Posting Komentar