Android: Differences between bound and started services - android

I am trying to identify the main conceptual (not implementation) differences between a bound and started service. Here are the main points:
A bound service allows extended two way communication between the activity and the service whereas a started service need not return any results to the client activity
A bound service will service multiple clients (as long as there is at least one client bound to it) while a started service performs a single operation and then shuts down. (I am aware that there can be started services which are also bound)
Are there any other major differences?

A service is a component that runs in the background to perform long-running operations without needing to interact with the user. For example, a service might play music in the background while the user is in a different application, or it might fetch data over the network without blocking user interaction with an activity. A service can essentially take two states:
Started : A service is started when an application component, such as an activity, starts it by calling startService(). Once started, a service can run in the background indefinitely, even if the component that started it is destroyed.
Bound : A service is bound when an application component binds to it by calling bindService(). A bound service offers a client-server interface that allows components to interact with the service, send requests, get results, and even do so across processes with interprocess communication (IPC).

See bellow image perhaps help to you :

The main difference is that bound service will get terminated by Android OS when the last client has unbound, started service however does not need any clients and it can run. As you already mentioned, you can also make a service that can support multi client communication, but is not bound
Difference also arises when you try to stop them. When you call stopService(..) on a bound service and it still has a client bound to it, nothing will happen while on the other hand, the started service will get terminated. When you call unbindService on a started service nothing happens, while if your service bound and this is the last client, it will shut down.... so over all the only difference between them is how they are started and stopped eventually
Apart from that, there is no difference.

Related

Why to use Android Bound Service?

I am curious to know why would you use Bound Service for 2 way interaction between Activity and Service when you can do the same kind of interaction with Started Service using local broadcasts and receivers defined in both Activity and Service
It will be helpful to know the pros and cons of each implementation.
I couldn't find any clear answer to this anywhere.
Using a bound Service is much more flexible. You can define methods on the Service (using AIDL) that return immediate results (synchronously), which you cannot do using LocalBroadcastManager. Using LocalBroadcastManager requires that you use your Service in a completely asynchronous way. This decouples the initiation of a service action with the return of the results (callback) which can make your code more complicated and more difficult to understand. Asynchronous use has some benefits, and there are places where you should use it, but if you have a bound Service you can choose exactly when to use synchronous calls and when to use asynchronous callbacks.
Also, the use of AIDL allows you to exactly describe the signatures of the services's method calls. If you use startService(Intent), you cannot guarantee that the caller will provide the correct arguments in the passed Intent, so you need to rely on the caller to "do the right thing" and/or you need to add a lot of additional argument verification.
Don't forget the comment from #CommonsWare about how LocalBroadcastManager only works if the Service is running in the same OS process as the rest of your app (which makes it unsuitable for programming things like system services, which are not running in your OS process).
A service is a component that runs in the background. It works to perform long-running operations without needing to interact with the user For example, a service might retrieve data over the network without stopping or blocking user interaction with an activity of an app or user may play music in the background while the user is in a different application. A service can be of two types or states:
Started: Once started, a service can run in the background indefinitely, even if the component which is started, is destroyed. A service is started when an app module or component, such as an activity, starts it by calling startService(). For example, retrieving contacts from phone book using service when Splash Screen starts.
Bound: A bound service offers a client-server interface that allows components to interact with the service, get results, send requests and even do so across processes with interprocess communication (IPC). A bound service is bound, when an app component binds to it by calling bindService().

Keep Service in sync with Application

I am wondering what is the best way to keep a service running while the application is running (may be in background) and then the service stopping when the application has ended (the application in this case has completely stopped, not in the background).
Another sub-question is: How to stop a service when application stops?
I realize one solution is to use BoundServices but is this the best way or good enough?
For example if an activity binds a service and then the system kills the activity and brings
it alive again then how will the service behave? Or are there other issues I am not aware of?
What would be the best way to implement this? Thanks.
Check out http://developer.android.com/guide/components/bound-services.html.
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.
You have two options to start the service:
1. bindService() to activity
2. startForeground() and bind while it runs
Option 1 will only run as long as the activity is in view and the runnable active. (example use would be to use service to download a file)
Option 2 will run as long as the application is running (even when the activity is in the background) This option requires that your service be listed in the notification bar.
You can have a service stop itself by calling stopSelf(int) (I dont think this works if a activity is currently bound) or you can call stopService() from an activity.
As for the system killing the activity question.... Without knowing what your service will be handling its hard to give advice on how to handle this situation. For the most part a service running in the foreground will be the last resource the system will try to reclaim. If the system kills the activity the services onDestory method will be called where you should do some clean up so that the next time it starts you can continue in a safe manor.
http://developer.android.com/images/service_lifecycle.png

Will a "foreground" bound service be stopped eventually because client will be stopped?

My requirement: Detect and respond to power events. I have an activity which allows the user to set up the responses. The app must keep responding after the UI has gone to the background (either by user action, ie "Home", or by, for example, receiving a phone call).
My analysis:
Use a BroadcastReceiver which accepts ACTION_POWER_DISCONNECTED (and ..CONNECTED)
I have implemented the receiver within the activity, and it works as expected. It continues to work in the background for some time, but eventually stops working because of normal android lifecycle, ie. the OS destroys it.
(Proposed) To keep the receiver running, I follow this recommendation and make it a service, with startForeground.
(Proposed) Because it must interact with the UI, ie, by accepting changes to its settings, it should be a bound service.
The expected problem:
According to the documents a bound service is stopped when all clients unbind. I have only one client (the UI), which will unbind in its onDestroy method. This will be called when client is stopped by the OS, which will eventually happen under normal resource management.
A service is "bound" when an application component binds to it by calling bindService(). A bound service offers a client-server interface that allows components to interact with the service, send requests, get results, and even do so across processes with interprocess communication (IPC). 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.
Questions
Is this analysis correct? ie. Will a bound service, with one UI client, eventually be destroyed when the client is destroyed, as part of normal OS management?
If so, what should I do about it?
Any other tips please :)
Asked as a hypothetical, because there is quite a bit of work setting up the bound service and then proving that it stops as expected. Also, even it it appears to work, I might be doing this wrong.
Why not register the broadcast receiver in your manifest? It will be started and invoked automatically when the power intents are broadcast. Then you don't need to worry about any sort of "keep alive" processing (and you won't need a service unless you need to do some longer processing)

When is it smart to use bindService and startService

I would like to know when it is smart to use bindService and when to use startService.
For example:
If I use bindService with BIND_AUTO_CREATE, the service will be started and created automatically as is written here: http://developer.android.com/reference/android/content/Context.html#BIND_AUTO_CREATE
When is it smart then to use bindService and when startService? I really don't understand these two correctly.
You usually use bindService() if your calling component(Activity) will need to communicate with the Service that you are starting, through the ServiceConnection. If you do not want to communicate with the Service you can use just startService(). You Can see below diffrence between service and bind service.
From the docs :
Started
A service is "started" when an application component (such as an activity) starts it by calling startService(). Once started, a service can run in the background indefinitely, even if the component that started it is destroyed. Usually, a started service performs a single operation and does not return a result to the caller. For example, it might download or upload a file over the network. When the operation is done, the service should stop itself.
Bound
A service is "bound" when an application component binds to it by calling bindService(). A bound service offers a client-server interface that allows components to interact with the service, send requests, get results, and even do so across processes with interprocess communication (IPC). 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.
You can read more here : Android Services, Bound Services
I agree with #Ovidiu Latcu but with one important note:
when using bound services, the service is ended when the activity that started it is ended, (if it is the only activity bound to that service).
So if you want to run your service at the background while the app is in the background,
(the activity is paused for example and not visible to the user) then you must start the service without bounding to it and communicate with it with BroadcastReceiver for example.

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?
Any tips, advice and helpful information are welcomed.
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):
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)

Categories

Resources