问题描述
这可能是一个愚蠢的问题,但我想知道何时使用 bindService
以及何时使用 startService
.
例如:
如果我将 bindService
与 BIND_AUTO_CREATE
一起使用,则该服务将按如下所示自动启动和创建:
什么时候使用 bindService
和 startService
明智?我真的不正确地理解这两个.
如果您的调用组件( Activity
)需要与以下组件通信,则通常使用 bindService()
通过 ServiceConnection
启动的 Service
.如果您不想与 Service
通信,则可以仅使用 startService()
.您可以在下面看到服务和绑定服务之间的区别.
从文档中:
This may be a stupid question but I would like to know when it is smart to use bindService
and when to use startService
.
For example:
If I use bindService
with BIND_AUTO_CREATE
, the service will be started and created automatically as is written here: http://developer.android.com/reference/android/content/Context.html#BIND_AUTO_CREATE
When is it smart then to use bindService
and when startService
? I really don't understand these two correctly.
You usually use bindService()
if your calling component(Activity
) will need to communicate with the Service
that you are starting, through the ServiceConnection
. If you do not want to communicate with the Service
you can use just startService()
. You Can see below diffrence between service and bind service.
From the docs :
You can read more here : Android Services, Bound Services
这篇关于什么时候明智地使用bindService和何时startService的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!