Monday, 20 February 2017

Bound services in Android, Types and uses

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
  • 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 
For more detail on JobScheduler visit my previous blogs on it.
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. 
As for example, download or upload a file over the network. When the operation is complete, the service should stop itself.

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
Important Point:
  • 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. 
Today Here I am going to discuss bound service only. I will wrote a separate blog for Started Service.

Implementation Steps
  1. A bound service is an implementation of the Service class that allows other applications to bind to it and interact with it. 
  2. To provide binding for a service, you must implement the onBind() callback method. 
  3. clients use IBinder object (return by onBind() method) to interact with the service. 
  4. A client binds to a service by calling bindService(). 
  5. Client must provide an implementation of ServiceConnection, which monitors the connection with the service. 
  6. The bindService() method returns immediately without a value 
  7. 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. 
  8. 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. 
  9. After that Android system delivers the same IBinder to any additional clients that bind, without calling onBind() again. 
  10. 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:
  1. Extending the Binder class 
  2. Using a Messenger 
  3. Using AIDL
Extending the Binder class 
  • 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.

Using a Messenger
  • 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.

Using AIDL
  • 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.
Diff b/w AIDL and Messenger
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..)



2 comments:

  1. Very useful information that you have shared and it is very useful to me.Thanks for sharing the information with us.

    best mobile app development company in chennai

    ReplyDelete
  2. There are very few Best Blockchain Development Company in Mohali. Being one,LBM Blockchain Solutions makes sure you are served the best. We are the Best Blockchain Development Company in Mohali. Our company has built an inevitable reputation in the industry with years of experience.

    ReplyDelete

Build a Custom Kernel Module for Android

Hi Guys!!!Hope you are doing well !!!. Today I will describe how you can write a custom kernel module(Hello world) for Android and load it a...