Service(服務(wù))是一種在后臺(tái)運(yùn)行,執(zhí)行長時(shí)間運(yùn)行的操作,無需與用戶交互的組件。例如,一個(gè)服務(wù)可以在后臺(tái)播放音樂,用戶在不同的應(yīng)用程序或者可能通過網(wǎng)絡(luò)獲取數(shù)據(jù),而不阻塞用戶交互活動(dòng)。本質(zhì)上,一個(gè)服務(wù)可以采取兩種狀態(tài):
| 狀態(tài) | 描述 |
|---|---|
| Started | 當(dāng)一個(gè)應(yīng)用程序組件,如活動(dòng),開始通過調(diào)用StartService()啟動(dòng)一個(gè)服務(wù)。開始以后服務(wù)可以無限期地在后臺(tái)運(yùn)行,即使啟動(dòng)它的組件被破壞。 |
| Bound | 當(dāng)一個(gè)應(yīng)用程序組件綁定調(diào)用bindService()方法綁定服務(wù)。綁定服務(wù)提供客戶端 - 服務(wù)器的接口,允許組件進(jìn)行交互的服務(wù),發(fā)送請(qǐng)求,得到結(jié)果,這樣可以跨進(jìn)程進(jìn)程間通信(IPC)。 |
每個(gè)服務(wù)都具有生命周期回調(diào)方法,可以實(shí)現(xiàn)監(jiān)視服務(wù)的狀態(tài)變化,并在適當(dāng)?shù)碾A段執(zhí)行工作。下圖左側(cè)顯示的整個(gè)生命周期由StartService()創(chuàng)建提供服務(wù) ,右邊的圖顯示bindService()創(chuàng)建的整個(gè)生命周期提供服務(wù):
要?jiǎng)?chuàng)建一個(gè)服務(wù),需要?jiǎng)?chuàng)建一個(gè)Java類,擴(kuò)展Service基類或者它的子類。Service基類定義各種回調(diào)方法,如下面表格給出。但是也并不需要實(shí)現(xiàn)所有的回調(diào)方法。重要的是要了解每一個(gè)變化以及實(shí)現(xiàn),以確保應(yīng)用程序能如用戶所期望的行為方式運(yùn)行。
| 回調(diào) | 描述 |
|---|---|
| onStartCommand() | 系統(tǒng)調(diào)用此方法當(dāng)另一組件,如一個(gè)活動(dòng),通過調(diào)用startService()要求該服務(wù)啟動(dòng)。如果要實(shí)現(xiàn)方法,它工作完成后停止服務(wù),通過調(diào)用stopSelf()或stopService()方法。 |
| onBind() | 該系統(tǒng)調(diào)用這個(gè)方法當(dāng)其他組件要通過調(diào)用bindService()綁定服務(wù)。如果實(shí)現(xiàn)此方法,必須提供客戶端與服務(wù)進(jìn)行通信,通過返回一個(gè)IBinder對(duì)象的接口。必須實(shí)現(xiàn)此方法,但如果不希望被綁定,那么應(yīng)該返回null。 |
| onUnbind() | 系統(tǒng)調(diào)用此方法,當(dāng)所有客戶都從服務(wù)發(fā)布的特定接口斷開。 |
| onRebind() | 該系統(tǒng)調(diào)用這個(gè)方法時(shí),新的客戶端已連接到服務(wù),它事先未通知,所有已經(jīng)上解除綁定后(意向)斷開它。 |
| onCreate() | 該系統(tǒng)調(diào)用時(shí),使用onStartCommand()或onBind()首先創(chuàng)建的服務(wù)這個(gè)方法。此調(diào)用需要執(zhí)行一次性安裝。 |
| onDestroy() | 系統(tǒng)調(diào)用這個(gè)方法當(dāng)服務(wù)不再使用(被銷毀)。服務(wù)應(yīng)該實(shí)現(xiàn)這個(gè)用于清理,如線程,注冊(cè)的偵聽器,接收器等任何資源 |
下面的主服務(wù)演示每一個(gè)方法生命周期:
package com.yiibai; import android.app.Service; import android.os.IBinder; import android.content.Intent; import android.os.Bundle; public class HelloService extends Service { /** indicates how to behave if the service is killed */ int mStartMode; /** interface for clients that bind */ IBinder mBinder; /** indicates whether onRebind should be used */ boolean mAllowRebind; /** Called when the service is being created. */ @Override public void onCreate() { } /** The service is starting, due to a call to startService() */ @Override public int onStartCommand(Intent intent, int flags, int startId) { return mStartMode; } /** A client is binding to the service with bindService() */ @Override public IBinder onBind(Intent intent) { return mBinder; } /** Called when all clients have unbound with unbindService() */ @Override public boolean onUnbind(Intent intent) { return mAllowRebind; } /** Called when a client is binding to the service with bindService()*/ @Override public void onRebind(Intent intent) { } /** Called when The service is no longer used and is being destroyed */ @Override public void onDestroy() { } }
這個(gè)例子將通過簡單的步驟顯示了如何創(chuàng)建Android服務(wù)。按照下面的步驟來修改前面章節(jié)創(chuàng)建的Android應(yīng)用程序 - Hello World示例 :
| 步驟 | 描述 |
|---|---|
| 1 | 使用Eclipse IDE創(chuàng)建Android應(yīng)用程序,并將其命名為HelloWorld在包c(diǎn)om.example.helloworld下,類似Hello World示例章節(jié)中一樣。 |
| 2 | 修改主要活動(dòng)文件MainActivity.java添加startService()和stopService()方法。 |
| 3 | 在包c(diǎn)om.example.helloworld下創(chuàng)建一個(gè)新的Java文件MyService.java。該文件將有實(shí)現(xiàn)Android服務(wù)相關(guān)的方法。 |
| 4 | 使用 <service.../>標(biāo)簽定義AndroidManifest.xml文件服務(wù)。一個(gè)應(yīng)用可以有一個(gè)或多個(gè)服務(wù),沒有任何限制。 |
| 5 | 修改res/layout/activity_main.xml文件的默認(rèn)內(nèi)容包括線性布局中的兩個(gè)按鈕。 |
| 6 | 定義兩個(gè)常量start_service和stop_service在 res/values/strings.xml 文件中 |
| 7 | 運(yùn)行該應(yīng)用程序啟動(dòng)Android模擬器并驗(yàn)證應(yīng)用程序所做的修改結(jié)果。 |
以下是改性主要活動(dòng)文件 src/com.example.helloworld/MainActivity.java 的內(nèi)容。這個(gè)文件包括每個(gè)基本的生命周期方法。添加 StartService() 和 stopService() 方法來啟動(dòng)和停止服務(wù)。
package com.example.helloworld; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.content.Intent; import android.view.View; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } // Method to start the service public void startService(View view) { startService(new Intent(getBaseContext(), MyService上一篇:Android TableLayout下一篇:Android Spinner