1、問題列表
1.1、service是運行在一個單獨的線程?
官方原文:
Caution: A
service runs in the main thread of its hosting process—the service does not create
its own thread and does not run
in a separate process (unless you specify otherwise). This means that, if your service is going to do any CPU intensive work or blocking operations (such as MP3 playback or networking), you should create a new thread within the service to do that work. By
using a separate thread, you will reduce the risk of Application Not Responding (ANR) errors and the application's main thread can remain dedicated to user interaction with your activities.
並不是運行在一個單獨的線程的,是運行在啓動他的組件的主線程上的。所以如果我們需要在service中做一些耗時的操作,需要自己另外啓一個線程。
1.2、service可以同時被startService()和bindService()啓動嗎?
官方原文:These two paths are not entirely separate. That is, you can bind to a service that was already started with
startService()
.
For example, a background music service could be started by calling startService()
with
anIntent
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.一個service可以同時被startService()和bindService()
1.2、service的聲明週期
2、demo
2.1、范德薩
3、相關鏈接
3.1、官方
Guide(Service):http://developer.android.com/guide/components/services.html#Notifications
Guide(BoundService):http://developer.android.com/guide/components/bound-services.html