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

試著忘記壹切 提交于 2019-12-17 15:29:06

问题


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

Thanks


回答1:


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




回答2:


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/




回答3:


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

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

  3. The Unbound service runs in the background indefinitely. Where As The Bound service does not run in the background indefinitely.

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




回答4:


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



来源:https://stackoverflow.com/questions/25240299/can-anybody-explain-what-is-difference-between-unbound-and-bound-service-in-andr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!