任何人都可以解释android中未绑定和绑定服务之间的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25240299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can anybody explain what is difference between unbound and bound service in android
提问by mohan
Can anybody explain what is difference between unbound and bound service in android and explain about intent service
任何人都可以解释android中未绑定和绑定服务之间的区别并解释意图服务
Thanks
谢谢
回答by Spurdow
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 EXCEPTIf the service was started by startService
当应用程序组件通过调用 bindService() 绑定到服务时,服务被绑定。绑定服务提供客户端-服务器接口,允许组件与服务交互、发送请求、获取结果,甚至通过进程间通信 (IPC) 跨进程执行此操作。
当最后一个客户端与服务解除绑定时,系统会销毁该服务EXCEPT如果该服务是由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.
当应用程序组件(例如活动)通过调用 startService() 启动它时,服务就会启动。服务一旦启动,就可以无限期地在后台运行,即使启动它的组件已被销毁。
BUT
但
Most confusion about the Service class actually revolves around what it is not:
关于 Service 类的大多数混淆实际上都围绕着它不是什么:
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).
服务不是一个单独的过程。Service 对象本身并不意味着它在自己的进程中运行;除非另有说明,否则它会在与其所属的应用程序相同的进程中运行。
服务不是线程。它本身并不是在主线程之外进行工作的一种方式(以避免应用程序无响应错误)。
That is where IntentServiceare used.
这就是使用IntentService 的地方。
IntentServiceis a subclass of Service that uses a worker thread to
IntentService是 Service 的子类,它使用工作线程来
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.
按需处理所有启动异步请求(表示为 Intent),一次一个。客户端通过 startService(Intent) 调用发送请求;该服务根据需要启动,依次使用工作线程处理每个 Intent,并在工作用完时自行停止。
hope it helps :)
希望能帮助到你 :)
回答by Saubhagya Ranjan Das
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/
有界服务绑定到绑定它的活动,并且仅在有界活动处于活动状态时才有效。而无界服务即使在活动被销毁后也会一直工作到完成。下面的链接给出了一个表格差异,这对采访非常有用 http://infoblogger.com/2014/04/15/bounded-service-in-android/
回答by Chinki Sai
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.
未绑定的服务在组件(如活动)调用 startService() 方法时启动 其中 As 当另一个组件(例如客户端)调用 bindService() 方法时,服务被绑定。
Unbound 服务可以通过调用 stopSelf() 方法停止自身。在所有客户端取消绑定服务之前,无法停止绑定服务。
Unbound 服务无限期地在后台运行。其中 As The Bound 服务不会无限期地在后台运行。
Unbound 服务由 stopService() 方法停止。在绑定服务中,客户端可以通过调用 unbindService() 方法解除绑定服务。
Thanks
谢谢
回答by Rohit Singh
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
服务可以是绑定或未绑定(启动)或两者兼而有之,这只是您提供给 Service 类的回调方法的实现问题。在此处查看所有四种回调方法
But for the sake of differentiationhere you go
但是为了区分,你去吧
1. Staring a service
1. 盯着服务
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
Unbound Service 通过调用startService()
方法启动。
绑定服务通过调用bindService()
方法启动。
但是在两次调用中,系统都在内部调用onStartCommand()方法
2. Life Span of a service
2. 服务的生命周期
Once an unboundService is started it runs indefinitely until
一旦 unboundService 启动,它就会无限期地运行,直到
- Application component calls stopService()method
- Service itself calls SelfStop()method.
- 应用程序组件调用stopService()方法
- 服务本身调用SelfStop()方法。
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
只要服务绑定到客户端,BoundService 就会运行。当没有与服务绑定的活动客户端时,系统销毁该服务
3. onBind() method
3. onBind() 方法
When you are writing a service you will have to override the onBind(). If
Unbound Service then return null
BoundService then return IBinder object.
在编写服务时,您必须覆盖 onBind()。如果
Unbound Service 则返回 null
BoundService 则返回 IBinder 对象。
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 BroadCastReceiveror ResultReceiver
尽管未绑定的服务不返回 Ibinder 对象,但这并不意味着它不能与应用程序组件交互。有一些方法可以做到这一点,例如BroadCastReceiver或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).
当您想要与您的服务进行双向通信时,您应该将您的服务与活动绑定。
例如。在后台播放音乐并暂停,播放选项(Activtiy <-> 服务)。
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).
您已经编写了一些处理位置更改的服务。
如果您想在移动 10 米时更新您的活动(使用未绑定的服务)。
如果您想在单击活动中的某个按钮时查看当前位置的坐标。(去绑定服务)。