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 to keep the connection on?

  • Qt for android: change the application icon
  • Android - screen orientation reloads activity
  • Is it possible to get last modified date from an assets file?
  • Can't scroll in a ListView in a swipeRefreshLayout
  • How to give animation to the ViewSwitcher
  • Is the static safe in Android?
  • Any tips, advice and helpful information are welcomed.

    Related posts:

    Scaling a drawable inside a button?
    Build error after migrating from Crashlytics SDK to Fabric
    Disable GoogleMaps v2 View so that it ignores all touch events
    Android integration with CCAvenue
    How to get ViewSwitcher to wrap only the currently displayed view height?
    Android AppWidgetManager method updateAppWidget fails to set intents, load data. And it happens rand...
  • Power-Off or Restart an android device via Code/Program?
  • How do I apply a style to all buttons of an Android application
  • Loop to upload list images one by one
  • How to hide StatusBar in Android 4
  • Search Dialog is not called onSearchRequested()
  • Commiting google-services.json to GitHub
  • 3 Solutions collect form web for “Service or Bound Service?”

    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.

    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

    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)

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.