Showing posts with label Service. Show all posts
Showing posts with label Service. Show all posts

Android local service binding - quick howto

So you've decided you need a Service in your Android app.
The question is how to invoke its methods? How to interact with it?

In a nutshell, you define an interface to the service.
Upon request of such an interface (by an activity), the code binds an asynchronous implementation of the interface to the real service and returns that implementation.
The activity invokes methods on that implementation (interface) and they are later (async) propagated to the service.
The result will be two toasts fired by the service, but originated from the activity.

Below is the how code.
When you test the code, don't forget to add the activity and service to the manifest file.
Enjoy!

Manifest:


Code: