Android Service系列(十二)创建bound Service

A bound service is one that allows application components to bind to it by calling bindService() to create a long-standing connection. It generally doesn't allow components to start it by calling startService().

翻译:通过bindService方法启动bound service  ,这是种长链接,startService通常不行。

Create a bound service when you want to interact with the service from activities and other components in your application or to expose some of your application's functionality to other applications through interprocess communication (IPC).

翻译:想要你的其他组件和service进行交互或者是想要暴露app的功能给其他应用,这个时候可以用bound service

 

To create a bound service, implement the onBind() callback method to return an IBinder that defines the interface for communication with the service. Other application components can then call bindService() to retrieve the interface and begin calling methods on the service. The service lives only to serve the application component that is bound to it, so when there are no components bound to the service, the system destroys it. You do not need to stop a bound service in the same way that you must when the service is started through onStartCommand().

翻译:要create bound service,你要实现onBind方法,返回一个IBinder,这个IBinder 定义了和这个service进行交互的接口。

其他引用可以通过 bindService()方法来获取这个接口并开始调用这个service的方法。

bound service 的生命周期取决于绑定它的组件,组件亡,service 亡。所以你不需要像start service那样关闭service.

 

To create a bound service, you must define the interface that specifies how a client can communicate with the service. This interface between the service and a client must be an implementation of IBinder and is what your service must return from the onBind() callback method. After the client receives the IBinder, it can begin interacting with the service through that interface.

翻译:为了create bound service,你要定义客户端和service进行交互的接口。这个接口需要是IBInder的实现,并且要在onBind()方法返回,客户端获取IBinder后,就可以通过这个接口和service进行交互了。

 

Multiple clients can bind to the service simultaneously. When a client is done interacting with the service, it calls unbindService() to unbind. When there are no clients bound to the service, the system destroys the service.

翻译:一个service可以同时绑定多个客户端。一个客户端done.则call unbindService() 来解绑。所有客户端都解绑了,则service

destroy.

 

There are multiple ways to implement a bound service, and the implementation is more complicated than a started service. For these reasons, the bound service discussion appears in a separate document about Bound Services.

翻译:Bound Service比较复杂,后文再叙。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值