Service是Android中四大組件之一,在Android開發中起到非常重要的作用,先來看一下官方對Service的定義:
A Service is an application component that can perform long-running operations in the background and does not provide a user interface. Another application component can start a service and it will continue to run in the background even if the user switches to another application. Additionally, a component can bind to a service to interact with it and even perform interprocess communication (IPC). For example, a service might handle network transactions, play music, perform file I/O, or interact with a content provider, all from the background.
翻譯過來就是:Service(服務)是一個沒有用戶界面的在後台運行執行耗時操作的應用組件。其他應用組件能夠啟動Service,並且當用戶切換到另外的應用場景,Service將持續在後台運行。另外,一個組件能夠綁定到一個service與之交互(IPC機制),例如,一個service可能會處理網絡操作,播放音樂,操作文件I/O或者與內容提供者(content provider)交互,所有這些活動都是在後台進行。
Service有兩種狀態,“啟動的”和“綁定”
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.
通過startService()啟動的服務處於“啟動的”狀態,一旦啟動,service就在後台運行,即使啟動它的應用組件已經被銷毀了。通常started狀態的service執行單任務並且不反悔任何結果給啟動者。比如當下載或上傳一個文件,當這項操作完成時,service應該停止它本身。
還有一種“綁定”狀態的service,通過調用bindService()來啟動,一個綁定的service提供一個允許組件與service交互的接口,可以發送請求、獲取返回結果,還可以通過誇進程通信來交互(IPC)。綁定的service只有當應用組件綁定後才能運行,多個組件可以綁定一個service,當調用unbind()方法時,這個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與activity一樣都存在與當前進程的主線程中,所以,一些阻塞UI的操作,比如耗時操作不能放在service裡進行,比如另外開啟一個線程來處理諸如網絡請求的耗時操作。如果在service裡進行一些耗CPU和耗時操作,可能會引發ANR警告,這時應用會彈出是強制關閉還是等待的對話框。所以,對service的理解就是和activity平級的,只不過是看不見的,在後台運行的一個組件,這也是為什麼和activity同被說為Android的基本組件。
Service生命周期中的一些方法:
通過這個圖可以看到,兩種啟動service的方式以及他們的聲明周期,bind service的不同之處在於當綁定的組件銷毀後,對應的service也就被kill了。service的聲明周期相比與activity的簡單了許多,只要好好理解兩種啟動service方式的異同就行。
service生命周期也涉及一些回調方法,這些方法都不用調用父類方法,具體如下: