Why would I use a Bound Service?

六眼飞鱼酱① 提交于 2019-12-04 09:12:36

There are a number of reasons for binding to a Service as opposed to sending it asynchronous messages. One important reason is it gives you more control of the lifetime of a service. If you are simply sending intents that are handled by the service, the service may well go away -- losing any internal state -- between messages. Bound services receive special treatment when Android is looking for resources that can be freed.

Another, unrelated, reason is if you are binding to an in-process service you can cast the IBinder to a known class and call methods on it directly. This provides a very rich (although tightly coupled) interface to the service. It would be difficult to simulate this rich interaction using message passing via Intents.

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