Android 中的 Service 物件

Android

簡介

使用方法

開發工具

開始寫程式

視覺化介面

視覺元件

對話元件

核心物件

事件處理

資料儲存

查詢功能

影音功能

繪圖功能

網路功能

衛星地圖

特殊功能

資源管理

裝置管理

系統核心

問題與回答

刷機升級

常用軟體

Eclipse

教學錄影

訊息

相關網站

參考文獻

最新修改

簡體版

English

原始文件:http://developer.android.com/guide/topics/fundamentals.html

Android 當中的服務是一種沒有介面,在背景端執行的程式。舉例而言,當系統正在執行其他工作時,某個服務可能正在播放背景音樂,或者取得網路上的資料以便計算出某些結果給其他 Activity。您可以繼承 Service 以建立服務類別。

A service doesn't have a visual user interface, but rather runs in the background for an indefinite period of time. For example, a service might play background music as the user attends to other matters, or it might fetch data over the network or calculate something and provide the result to activities that need it. Each service extends the Service base class.

音樂播放程式是一個典型的服務,有許多 Activity 都可能會選取音樂並要求播放。但是,即使在要求播放的 Activity 結束之後,音樂播放服務仍然繼續在背景運作,而不會因為該 Activity 的結束而關閉。

A prime example is a media player playing songs from a play list. The player application would probably have one or more activities that allow the user to choose songs and start playing them. However, the music playback itself would not be handled by an activity because users will expect the music to keep playing even after they leave the player and begin something different. To keep the music going, the media player activity could start a service to run in the background. The system would then keep the music playback service running even after the activity that started it leaves the screen.

您可以連接到一個正在執行中的服務,然後透過介面函數與該服務溝通。舉例而言,您可以呼叫音樂播放程式的 pause、rewind、stop、restart 或 playback 等函數,以控制該服務的運行。

It's possible to connect to (bind to) an ongoing service (and start the service if it's not already running). While connected, you can communicate with the service through an interface that the service exposes. For the music service, this interface might allow users to pause, rewind, stop, and restart the playback.

就像 Activity 或其他的元件一樣,服務是一個行程中的主要執行緒 (main thread),所以不會影像到其他元件或介面的運作,如果服務需要執行一個較耗費時間的工作,就會建立一個新的 thread 去處理該工作。

Like activities and the other components, services run in the main thread of the application process. So that they won't block other components or the user interface, they often spawn another thread for time-consuming tasks (like music playback). See Processes and Threads, later.

服務可以透過呼叫 stopSelf() 函數自行中止,或者被 Context.stopSerivce() 函數所中止。

A service can be stopped by calling its stopSelf() method, or by calling Context.stopService().

Service 的生命週期

service_lifecycle.png

圖一、Service 的生命週期狀態圖

參考文獻

  1. Android底層結構:SDK Service線程知多少?(5) — http://www.android1.net/Topic.aspx?BoardID=11&TopicID=909
    • 解釋了如何用 Activity 綁定 Service 並取得回呼訊息。
  2. How-to use Services in our Android Application: Android Programming Environment Tutorial — http://www.brighthub.com/mobile/google-android/articles/34861.aspx
  3. Android Service creation and consumption , Posted August 7th, 2008 by Nazmul — http://developerlife.com/tutorials/?p=356
  4. Android底層結構:SDK Service線程知多少?(1), By 高煥堂 — http://www.android1.net/Topic.aspx?BoardID=11&TopicID=775
  5. 主题:Android Services使用示例 — http://www.javaeye.com/topic/568547

Facebook

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License