Android services and deference [duplicate] - android

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).

Related

Can anybody explain what is difference between unbound and bound service in android

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).

Difference between onStartCommand() and onBind()

How is the "bind" action of the onBind() method different than just calling onStartCommand() ?
onStartCommand()
"The system calls this method when another component, such as an activity, requests that the service be started, by calling startService()."
onBind()
The system calls this method when another component wants to bind with the service (such as to perform RPC), by calling bindService().
I want to write a chat client service which receives messages from multiple users. Which function would be more appropriate?
The first (onStartCommand()) is called when your Service begins to do its work. onCreate() has completed and it is ready to get to doing what needs to be done.
The second (onBind()) is called when another Thread registers to connect to the Service so that they can communicate. You would configure or set up the means for the communication in here such as Interface validation or calls back to the registering Activity.
Binding allows you to tie the Service to the lifespan of, for example, an Activity. If the Activity completes then the Service is allowed to be released and can itself finish. The Service will last as long as there is something still bound to it.
onStartCommand() and onBind() are callback methods of Service class.
onStartCommand() called after onCreate() method of Service class first time.Next time whenever any other android component start same service then Service received new request in onStartCommand() method.
onBind() called when another Android components try to connect with already running Service by using bindService() method .Its used to pass some new info to service or try to make Service connection.
A bound service will end when it has no more activities bound to it. Binding also allows you to send additional commands to it via interfaces like AIDL. In your case, I think you'd want a bound service, as you likely don't want the service to outlive the activity.
In fact, if a component calls bindService() to create the service and onStartCommand() is not called, the service runs only as long as the component is bound to it. After the service is unbound from all of its clients, the system destroys it. So, by onBind() if the activity destroy your service would not trigger.
According to the official documentation,
when a service is started using bindService()
if a component(i.e Activity) calls bindService() to create the service and onStartCommand() is not called, the service runs only as long as the component is bound to it. After the service is unbound from all of its clients, the system destroys it.
and when a service is started using startService()
If a component starts the service by calling startService() (which results in a call to onStartCommand()), the service continues to run until it stops itself with stopSelf() or another component stops it by calling stopService().

Android service lifecycle

In one case, one service being started by another component runs on the main UI thread of that component, while the service may live even that component is destroyed, so my question is where to execute the service if the component started that service is stopped?
Typically, when you call startService(), the service will remain running until you call stopService() from another component or stopSelf() from the service itself. And onStartCommand() will always run on UI thread.
If the service is running, subsequent calls to startService() will not create another instance of your service but run onStartCommand() again on the running one. This is slightly different if you are binding your component to a service. In this case, the service is automatically destroyed when you unbind all components. For more details on this, see: http://developer.android.com/guide/components/services.html#Lifecycle.
In addition, note that there different ways to keep a service running, depending on what you return from the onStartCommand():
START_STICKY is used for services that are explicitly started and
stopped as needed, while START_NOT_STICKY or START_REDELIVER_INTENT
are used for services that should only remain running while processing
any commands sent to them.
To answer your question specifically, where and how to start your service depends on what exactly you want to do with it. If the component that started the service stopped, it is up to you to either 1) get a new reference to the service from another component and stop it or 2) stop the service from the service itself. But the service won't stop because the component did. Unless you are binding it to the service.

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.

Getting data from an Unbound Service in Android

I currently I have an unbound service that is running continually grabbing my gps position that I start on boot. I then have an app that is suppose to plot where I've been by pulling data from the service.
I can't bind the the service to talk to it or it will be destroyed once I close the app.
Is there any good way to get data from an unbound service or keep a bound service from dying once I unbind it?
Cheers! :)
There is a workaround for keeping your service alive. Call your service by calling startService and then bind to the service. This way your activity is maintaining the the lifecycle of the service.
As the documentation states:
A service can be both started and have
connections bound to it. In such a
case, the system will keep the service
running as long as either it is
started or there are one or more
connections
So first, start the service with startService(), then bind to it in onResume(), and unbind in onPause(). The service will continue to run nevertheless because it is started. And when you want to stop the service either call stopSelf() from within the service or stopService() from an activity. It will stop right away or as soon as you unbind from it if there's any connection alive.

Categories

Resources