Skip to content Skip to sidebar Skip to footer

Service Or Bound Service?

I'm creating an application that connects to an XMPP server on Android. I want to keep the connection on till the user logs out. Should I use a regular Service or a Bound Service t

Solution 1:

I like this explanation:

Started services are easy to program for simple one way interactions from an activity to a service, however, they require more complex and ad hoc programming for extended two-way conversations with their clients.

In contrast, bound services may be a better choice for more complex two-way interactions between activities and services. For example, they support two-way conversations.

So, as you said, If you want to interact with the service use bound service. With started services (or intent services) you could do it, only it would require more complex programming. (by Douglas Schmidt: https://www.youtube.com/watch?v=cRFw7xaZ_Mg (11'10'')):

Here is a summary that helped me understand (thanks Doug): enter image description here

Finally, one last link that helped me also: http://www.techotopia.com/index.php/An_Overview_of_Android_Started_and_Bound_Services

Started services are launched by other application components (such as an activity or even a broadcast receiver) and potentially run indefinitely in the background until the service is stopped, or is destroyed by the Android runtime system in order to free up resources. A service will continue to run if the application that started it is no longer in the foreground, and even in the event that the component that originally started the service is destroyed

A bound service is similar to a started service with the exception that a started service does not generally return results or permit interaction with the component that launched it. A bound service, on the other hand, allows the launching component to interact with, and receive results from, the service.

Solution 2:

A bound service is the server in a client-server interface. A bound service allows components (such as activities) to bind to the service, send requests, receive responses, and even perform interprocess communication (IPC). A bound service typically lives only while it serves another application component and does not run in the background indefinitely.

If all the code exists in one activity from user connected to user logout then go for bound service

But if it is code exists in multiple activities try with service

Solution 3:

I found out the difference between the two and when to use it. If you want to interact with the service (for example send arguments etc), use bound service and it return the service object in the onServiceConnected method (where you can call methods in the service). You cannot interact with a regular service (from another class)

Post a Comment for "Service Or Bound Service?"