gpt4 book ai didi

android - 什么时候使用 bindService 和 startService 比较聪明

转载 作者:IT王子 更新时间:2023-10-28 23:45:44 26 4
gpt4 key购买 nike

我想知道什么时候用bindService比较聪明,什么时候用startService

例如:

如果我将 bindServiceBIND_AUTO_CREATE 一起使用,服务将自动启动并创建,如下所示:http://developer.android.com/reference/android/content/Context.html#BIND_AUTO_CREATE

什么时候使用 bindService 以及什么时候使用 startService?这两个我真的不明白。

最佳答案

如果您的调用组件(Activity)需要通过ServiceConnection。如果您不想与 Service 通信,您可以只使用 startService()。您可以在下面看到服务和绑定(bind)服务之间的差异。

来自文档:

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.

您可以在此处阅读更多信息:Android Services , Bound Services

关于android - 什么时候使用 bindService 和 startService 比较聪明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13787460/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com