startService和bindService最好不要混用,当然,如果你对Servie的生命周期烂熟于心的话,你想怎么用就怎么用。
个人建议还是不要混用,以免思维混乱,吧自己搞的云里雾里的。
使用startService启动的Service,在调用方销毁后,Service任然运行,除非调用自己调用stopSelf或者外部调用stopService()
主要,如果某个app中Service没有销毁的话,这个app也不会销毁,常驻内存。
官方文档说的最好: http://developer.android.com/guide/components/services.html
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.
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.
startService()
. For example, a background music service could be started by calling
startService()
with an
Intent
that identifies the music to play. Later, possibly when the user wants to exercise some control over the player or get information about the current song, an activity can bind to the service by calling
bindService()
. In cases like this,
stopService()
or
stopSelf()
does not actually stop the service until all clients unbind.