cheap jerseys
Android Local Bound Services – A Worked Example | Timesmagazine24
AppsDigital MarketingEntertainmentLifestyleNewsTechnologyWebsite

Android Local Bound Services – A Worked Example

As outlined in some detail in the previous chapters, bound services, unlike started services, provide a mechanism for implementing communication between an Android service and one or more client components. The objective of this chapter is to build on the overview of bound services provided in An Overview of Android Started and Bound Services before embarking on an example implementation of a local bound service in action.

Understanding Bound Services In common with started services, bound services are provided to allow applications to perform tasks in the background. Unlike started services, however, multiple client components may bind to a bound service and, once bound, interact with that service using a variety of different mechanisms. Bound services are created as sub-classes of the Android Service class and must, at a minimum, implement the onBind() method.

Client components bind to a service via a call to the bindService() method. The first bind request to a bound service will result in a call to that service’s onBind() method (subsequent bind request do not trigger an onBind() call). Clients wishing to bind to a service must also implement a ServiceConnection subclass containing onServiceConnected() and onServiceDisconnected() methods which will be called once the client-server connection has been established or disconnected respectively. In the case of the onServiceConnected() method, this will be passed an IBinder object containing the information needed by the client to interact with the service.

Bound Service Interaction Options There are two recommended mechanisms for implementing interaction between client components and a bound service. In the event that the bound service is local and private to the same application as the client component (in other words it runs within the same process and is not available to components in other applications.

The recommended mechanism is to create a subclass of the Binder class and extend it to provide an interface to the service. An instance of this Binder object is then returned by the onBind() method and subsequently used by the client component to directly access methods and data held within the service.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top button

cheap jerseys