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.
Related
In the bound services documentation, it says
When a service is unbound from all clients, the Android system destroys it (unless it was also started with a startService() call). As such, you don't have to manage the lifecycle of your service if it's purely a bound service—the Android system manages it for you based on whether it is bound to any clients.
However, if you choose to implement the onStartCommand() callback method, then you must explicitly stop the service, because the service is now considered to be started. In this case, the service runs until the service stops itself with stopSelf() or another component calls stopService(), regardless of whether it is bound to any clients.
Does this mean that simply implementing onStartCommand automagically puts the service into a started state when a client binds to it?
I've been looking for a way to guarentee that when a client binds to the service, the service is in the started state. So far, the only way I've been able to do it is by having the client call startService followed by bindService. But if the above is correct, then all I have to do is implement onStartCommand and the client is free to just call bindService.
I agree that the documentation is wrong. Simply implementing onStartCommand() changes nothing. Especially because you don't implement it, you override the method, because there is already an existing default implementation of onStartCommand()
When you bind to a Service, the Service isn't technically "started", it is just bound. onStartCommand() will not be called unless something calls startService().
Can anybody explain what is difference between unbound and bound service in android and explain about intent service
Thanks
Bound Service
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).
When the last client unbinds from the service, the system destroys the
service EXCEPT If the service was started by
startService
Unbound Service or 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.
BUT
Most confusion about the Service class actually revolves around what it is not:
A Service is not a separate process. The Service object itself does
not imply it is running in its own process; unless otherwise
specified, it runs in the same process as the application it is part
of.
A Service is not a thread. It is not a means itself to do work off of
the main thread (to avoid Application Not Responding errors).
That is where IntentService are used.
IntentService is a subclass of Service that uses a worker thread to
handle all start asynchronous requests (expressed as Intents) on
demand, one at a time. Clients send requests through
startService(Intent) calls; the service is started as needed, handles
each Intent in turn using a worker thread, and stops itself when it
runs out of work.
hope it helps :)
Bounded services are bounded to an activity which binds it and will work only till bounded activity is alive.
while a unbounded service will work till the completion even after activity is destroyed.
a tabular difference is given in below link which is very useful for interviews
http://infobloggall.com/2014/04/15/bounded-service-in-android/
Unbound service is started when component (like activity) calls startService() method
Where As A service is bound when another component (e.g. client) calls bindService() method.
The Unbound service can stop itself by calling the stopSelf() method.
Where As The Bound service cannot be stopped until all clients unbind the service.
The Unbound service runs in the background indefinitely.
Where As The Bound service does not run in the background indefinitely.
The Unbound service is stopped by stopService() method.
Where As In The Bound service, The client can unbind the service by calling the unbindService() method.
Thanks
Bound and Unbound Services are not two sides of a coin
A service can be a bound or unbound(started) or both, It is just the matter of implementation you provide to the callback methods of Service class. See all four callback methods here
But for the sake of differentiation here you go
1. Staring a service
Unbound Service is started by calling startService() method.
Bound Service is started by calling bindService() method.
However in both calls system calls onStartCommand() method internally
2. Life Span of a service
Once an unboundService is started it runs indefinitely until
Application component calls stopService() method
Service itself calls SelfStop() method.
BoundService runs as long as the service is bound to a client. When there is no active client bound with the service, the system destroys the Service
3. onBind() method
When you are writing a service you will have to override the onBind(). If
Unbound Service then return null
BoundService then return IBinder object.
Though unbound services does not return Ibinder object it does not mean that it can not interact with application component.
There are ways to do that for example BroadCastReceiver or ResultReceiver
One way vs Two-way communication with Service
When you want two-way communication with your Service then you should bind your service with Activity.
Eg. Playing music in the background with pause, play option (Activtiy <-> Service).
Go with unbound or started service when you just want your Service to update your Activity (Service->Activity).
Eg: Timer Service which updates Activity every second.
Another example
You have written some Service which deals with Location changes.
If you want to update your activity when you move 10 meters (Go with unbound service).
If you want to see the coordinates of your current location when you click some button in the activity. (Go with the bound service).
I have this "Activity 1" binded to Service. I started the service using
Intent wrapperServiceIntent = new Intent(mContext,BleWrapper.class);
bindService(wrapperServiceIntent,mBLEWrapperServiceConnection,BIND_AUTO_CREATE);
Can I stop this service using
stopService(new Intent(mContext,BleWrapper.class));
insted of
mContext.unbindService(mServiceConnection);
Is the above scenario possible?
From documentation:
When a service is unbound from all clients, the Android system destroys it (unless it was also started with onStartCommand()). As such, you don't have to manage the lifecycle of your service if it's purely a bound service—the Android system manages it for you based on whether it is bound to any clients.
However, if you choose to implement the onStartCommand() callback method, then you must explicitly stop the service, because the service is now considered to be started. In this case, the service runs until the service stops itself with stopSelf() or another component calls stopService(), regardless of whether it is bound to any clients.
No you can not do it instead, you need to remove the bindings first.
The Android documentation says:
Note that if a stopped service still has ServiceConnection objects bound to it with the BIND_AUTO_CREATE set, it will not be destroyed until all of these bindings are removed.
Another thing, in stopService you need to pass mServiceConnection instead of creating a new intent.
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.
Can anybody explain what is difference between unbound and bound service in android and explain about intent service
Thanks
Bound Service
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).
When the last client unbinds from the service, the system destroys the
service EXCEPT If the service was started by
startService
Unbound Service or 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.
BUT
Most confusion about the Service class actually revolves around what it is not:
A Service is not a separate process. The Service object itself does
not imply it is running in its own process; unless otherwise
specified, it runs in the same process as the application it is part
of.
A Service is not a thread. It is not a means itself to do work off of
the main thread (to avoid Application Not Responding errors).
That is where IntentService are used.
IntentService is a subclass of Service that uses a worker thread to
handle all start asynchronous requests (expressed as Intents) on
demand, one at a time. Clients send requests through
startService(Intent) calls; the service is started as needed, handles
each Intent in turn using a worker thread, and stops itself when it
runs out of work.
hope it helps :)
Bounded services are bounded to an activity which binds it and will work only till bounded activity is alive.
while a unbounded service will work till the completion even after activity is destroyed.
a tabular difference is given in below link which is very useful for interviews
http://infobloggall.com/2014/04/15/bounded-service-in-android/
Unbound service is started when component (like activity) calls startService() method
Where As A service is bound when another component (e.g. client) calls bindService() method.
The Unbound service can stop itself by calling the stopSelf() method.
Where As The Bound service cannot be stopped until all clients unbind the service.
The Unbound service runs in the background indefinitely.
Where As The Bound service does not run in the background indefinitely.
The Unbound service is stopped by stopService() method.
Where As In The Bound service, The client can unbind the service by calling the unbindService() method.
Thanks
Bound and Unbound Services are not two sides of a coin
A service can be a bound or unbound(started) or both, It is just the matter of implementation you provide to the callback methods of Service class. See all four callback methods here
But for the sake of differentiation here you go
1. Staring a service
Unbound Service is started by calling startService() method.
Bound Service is started by calling bindService() method.
However in both calls system calls onStartCommand() method internally
2. Life Span of a service
Once an unboundService is started it runs indefinitely until
Application component calls stopService() method
Service itself calls SelfStop() method.
BoundService runs as long as the service is bound to a client. When there is no active client bound with the service, the system destroys the Service
3. onBind() method
When you are writing a service you will have to override the onBind(). If
Unbound Service then return null
BoundService then return IBinder object.
Though unbound services does not return Ibinder object it does not mean that it can not interact with application component.
There are ways to do that for example BroadCastReceiver or ResultReceiver
One way vs Two-way communication with Service
When you want two-way communication with your Service then you should bind your service with Activity.
Eg. Playing music in the background with pause, play option (Activtiy <-> Service).
Go with unbound or started service when you just want your Service to update your Activity (Service->Activity).
Eg: Timer Service which updates Activity every second.
Another example
You have written some Service which deals with Location changes.
If you want to update your activity when you move 10 meters (Go with unbound service).
If you want to see the coordinates of your current location when you click some button in the activity. (Go with the bound service).