服務(wù)基本上分為兩種形式
- 啟動
當應(yīng)用組件(如 Activity)通過調(diào)用 startService() 啟動服務(wù)時,服務(wù)即處于“啟動”狀態(tài)。
- 綁定
當應(yīng)用組件通過調(diào)用 [bindService()](http://developer.android.com/reference/android/content/Context.html#bindService(android.content.Intent, android.content.ServiceConnection, int)) 綁定服務(wù)時,服務(wù)即處于“綁定”狀態(tài)。
綁定服務(wù)提供了一個客戶端-服務(wù)器接口,允許組件與服務(wù)進行交互、發(fā)送請求、獲取結(jié)果,甚至是利用進程間通信 (IPC) 跨進程執(zhí)行這些操作;僅當與另一個應(yīng)用組件綁定時,綁定服務(wù)才會運行。
應(yīng)重寫的最重要的回調(diào)方法包括:
- [onStartCommand()](http://developer.android.com/reference/android/app/Service.html#onStartCommand(android.content.Intent, int, int))
啟動服務(wù)startService() -> 重寫 [onStartCommand()](http://developer.android.com/reference/android/app/Service.html#onStartCommand(android.content.Intent, int, int)) 一旦執(zhí)行此方法,服務(wù)即會啟動并可在后臺無限期運行。 如果已實現(xiàn)此方法,則在服務(wù)工作完成后,需要通過調(diào)用 stopSelf()
或stopService()來停止服務(wù)。(如果您只想提供綁定,則無需實現(xiàn)此方法。)
綁定服務(wù) [bindService()](http://developer.android.com/reference/android/content/Context.html#bindService(android.content.Intent, android.content.ServiceConnection, int)),系統(tǒng)將調(diào)用此方法onBind()。在此方法的實現(xiàn)中,必須通過返回 IBinder提供一個接口,供客戶端用來與服務(wù)進行通信。請務(wù)必實現(xiàn)此方法,但如果并不希望允許綁定,則應(yīng)返回 null。
首次創(chuàng)建服務(wù)時,系統(tǒng)將調(diào)用此方法來執(zhí)行一次性設(shè)置程序(在調(diào)用 [onStartCommand()](http://developer.android.com/reference/android/app/Service.html#onStartCommand(android.content.Intent, int, int))
或 onBind()
之前)。如果服務(wù)已在運行,則不會調(diào)用此方法。
當服務(wù)不再使用且將被銷毀時,系統(tǒng)將調(diào)用此方法。服務(wù)應(yīng)該實現(xiàn)此方法來清理所有資源,如線程、注冊的偵聽器、接收器等。 這是服務(wù)接收的最后一個調(diào)用。
如果組件通過調(diào)用 startService()啟動服務(wù)(這會導致對 [onStartCommand()](http://developer.android.com/reference/android/app/Service.html#onStartCommand(android.content.Intent, int, int))的調(diào)用),則服務(wù)將一直運行,直到服務(wù)使用 stopSelf()自行停止運行,或由其他組件通過調(diào)用 stopService()停止它為止。
如果組件是通過調(diào)用 [bindService()](http://developer.android.com/reference/android/content/Context.html#bindService(android.content.Intent, android.content.ServiceConnection, int))來創(chuàng)建服務(wù)(且未調(diào)用 [onStartCommand()](http://developer.android.com/reference/android/app/Service.html#onStartCommand(android.content.Intent, int, int))),則服務(wù)只會在該組件與其綁定時運行。一旦該服務(wù)與所有客戶端之間的綁定全部取消,系統(tǒng)便會銷毀它。
為了確保應(yīng)用的安全性,**請始終使用顯式 Intent 啟動或綁定 Service
**,且不要為服務(wù)聲明 Intent 過濾器。
創(chuàng)建啟動服務(wù)
從傳統(tǒng)上講,可以擴展Service , IntentService兩個類
1. Service
這是適用于所有服務(wù)的基類。擴展此類時,必須創(chuàng)建一個用于執(zhí)行所有服務(wù)工作的新線程,因為默認情況下,服務(wù)將使用應(yīng)用的主線程,這會降低應(yīng)用正在運行的所有 Activity 的性能。
擴展 Service類來創(chuàng)建啟動服務(wù) :
該基類包含更多代碼,但如需同時處理多個啟動請求,則更適合使用該基類;
使用 IntentService顯著簡化了啟動服務(wù)的實現(xiàn)。但是,若要求服務(wù)執(zhí)行多線程(而不是通過工作隊列處理啟動請求),則可擴展 Service類來處理每個 Intent。
為了便于比較,以下提供了 Service 類實現(xiàn)的代碼示例,該類執(zhí)行的工作與使用 IntentService的示例完全相同。
也就是說,對于每個啟動請求,它均使用工作線程執(zhí)行作業(yè),且每次僅處理一個請求。
以下是使用 Service類的代碼示例:
public class HelloService extends Service {
private Looper mServiceLooper;
private ServiceHandler mServiceHandler;
private final class ServiceHandler extends Handler {
public ServiceHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
long endTime = System.currentTimeMillis() + 5 * 1000;
while (System.currentTimeMillis() < endTime) {
synchronized (this) {
try {
wait(endTime - System.currentTimeMillis());
} catch (Exception e) {
}
}
}
stopSelf(msg.arg1);
}
}
@Override
public void onCreate() {
HandlerThread thread = new HandlerThread("ServiceStartArguments",
Process.THREAD_PRIORITY_BACKGROUND);
thread.start();
mServiceLooper = thread.getLooper();
mServiceHandler = new ServiceHandler(mServiceLooper);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
Message msg = mServiceHandler.obtainMessage();
msg.arg1 = startId;
mServiceHandler.sendMessage(msg);
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show();
}
}```
###2. [IntentService](http://developer.android.com/reference/android/app/IntentService.html)
這是 [Service](http://developer.android.com/reference/android/app/Service.html)的子類,它使用工作線程逐一處理所有啟動請求。如果您不要求服務(wù)同時處理多個請求,這是最好的選擇。 您只需實現(xiàn) [onHandleIntent()](http://developer.android.com/reference/android/app/IntentService.html#onHandleIntent(android.content.Intent))方法即可,該方法會接收每個啟動請求的 Intent,使您能夠執(zhí)行后臺工作。
擴展[IntentService](http://developer.android.com/reference/android/app/IntentService.html)創(chuàng)建啟動服務(wù),執(zhí)行以下操作:
> - 創(chuàng)建默認的工作線程,用于在應(yīng)用的主線程外執(zhí)行傳遞給 [onStartCommand()](http://developer.android.com/reference/android/app/Service.html#onStartCommand(android.content.Intent, int, int))的所有 Intent。
- 創(chuàng)建工作隊列,用于將一個 Intent 逐一傳遞給 [onHandleIntent()](http://developer.android.com/reference/android/app/IntentService.html#onHandleIntent(android.content.Intent))實現(xiàn),這樣您就永遠不必擔心多線程問題。
- 在處理完所有啟動請求后停止服務(wù),因此您永遠不必調(diào)用 [stopSelf()](http://developer.android.com/reference/android/app/Service.html#stopSelf())。
- 提供 [onBind()](http://developer.android.com/reference/android/app/IntentService.html#onBind(android.content.Intent))的默認實現(xiàn)(返回 null)。
- 提供 [onStartCommand()](http://developer.android.com/reference/android/app/IntentService.html#onStartCommand(android.content.Intent, int, int))的默認實現(xiàn),可將 Intent 依次發(fā)送到工作隊列和 [onHandleIntent()](http://developer.android.com/reference/android/app/IntentService.html#onHandleIntent(android.content.Intent))實現(xiàn)。
---
綜上所述 :
只需實現(xiàn) [onHandleIntent()](http://developer.android.com/reference/android/app/IntentService.html#onHandleIntent(android.content.Intent))來完成客戶端提供的工作即可。(不過,您還需要為服務(wù)提供小型構(gòu)造函數(shù)。)
以下是 [IntentService](http://developer.android.com/reference/android/app/IntentService.html)的實現(xiàn)示例:
```java
public class HelloIntentService extends IntentService {
public HelloIntentService() {
super("HelloIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
long endTime = System.currentTimeMillis() + 5 * 1000;
while (System.currentTimeMillis() < endTime) {
synchronized (this) {
try {
wait(endTime - System.currentTimeMillis());
} catch (Exception e) {
}
}
}
}
}
-- 您只需要一個構(gòu)造函數(shù)和一個 onHandleIntent()實現(xiàn)即可。
如果您決定還重寫其他回調(diào)方法(如 onCreate()、[onStartCommand()](http://developer.android.com/reference/android/app/IntentService.html#onStartCommand(android.content.Intent, int, int))或 onDestroy()),請確保調(diào)用超類實現(xiàn),以便 IntentService能夠妥善處理工作線程的生命周期。
例如,[onStartCommand()](http://developer.android.com/reference/android/app/IntentService.html#onStartCommand(android.content.Intent, int, int))必須返回默認實現(xiàn)(即,如何將 Intent 傳遞給 onHandleIntent()):
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
return super.onStartCommand(intent,flags,startId);
}
除 onHandleIntent()之外,您無需從中調(diào)用超類的唯一方法就是 onBind()
(僅當服務(wù)允許綁定時,才需要實現(xiàn)該方法)。
啟動服務(wù)
可以通過將 [Intent](http://developer.android.com/reference/android/content/Intent.html)(指定要啟動的服務(wù))傳遞給 startService(),從 Activity 或其他應(yīng)用組件啟動服務(wù)。Android 系統(tǒng)調(diào)用服務(wù)的 [onStartCommand()](http://developer.android.com/reference/android/app/Service.html#onStartCommand(android.content.Intent, int, int))
方法,并向其傳遞 Intent。(切勿直接調(diào)用[onStartCommand()](http://developer.android.com/reference/android/app/Service.html#onStartCommand(android.content.Intent, int, int))。)
例如,Activity 可以結(jié)合使用顯式 Intent 與 startService()
,啟動上文中的示例服務(wù) (HelloSevice):
ntent intent = new Intent(this, HelloService.class);startService(intent);
startService()方法將立即返回,且 Android 系統(tǒng)調(diào)用服務(wù)的 [onStartCommand()](http://developer.android.com/reference/android/app/Service.html#onStartCommand(android.content.Intent, int, int))方法。如果服務(wù)尚未運行,則系統(tǒng)會先調(diào)用 onCreate(),然后再調(diào)用 [onStartCommand()](http://developer.android.com/reference/android/app/Service.html#onStartCommand(android.content.Intent, int, int))。
如果服務(wù)亦未提供綁定,則使用 startService()傳遞的 Intent 是應(yīng)用組件與服務(wù)之間唯一的通信模式。但是,如果您希望服務(wù)返回結(jié)果,則啟動服務(wù)的客戶端可以為廣播創(chuàng)建一個 PendingIntent(使用[getBroadcast()](http://developer.android.com/reference/android/app/PendingIntent.html#getBroadcast(android.content.Context, int, android.content.Intent, int))),并通過啟動服務(wù)的 Intent傳遞給服務(wù)。然后,服務(wù)就可以使用廣播傳遞結(jié)果。
多個服務(wù)啟動請求會導致多次對服務(wù)的 [onStartCommand()](http://developer.android.com/reference/android/app/Service.html#onStartCommand(android.content.Intent, int, int))進行相應(yīng)的調(diào)用。但是,要停止服務(wù),只需一個服務(wù)停止請求(使用 stopSelf() 或 stopService())即可。
停止服務(wù)
啟動服務(wù)必須管理自己的生命周期。也就是說,除非系統(tǒng)必須回收內(nèi)存資源,否則系統(tǒng)不會停止或銷毀服務(wù),而且服務(wù)在 [onStartCommand()](http://developer.android.com/reference/android/app/Service.html#onStartCommand(android.content.Intent, int, int))返回后會繼續(xù)運行。因此,服務(wù)必須通過調(diào)用 stopSelf()
自行停止運行,或者由另一個組件通過調(diào)用 stopService()來停止它。
一旦請求使用 stopSelf()或 stopService()停止服務(wù),系統(tǒng)就會盡快銷毀服務(wù)。
但是,如果服務(wù)同時處理多個 [onStartCommand()](http://developer.android.com/reference/android/app/Service.html#onStartCommand(android.content.Intent, int, int))請求,則您不應(yīng)在處理完一個啟動請求之后停止服務(wù),因為您可能已經(jīng)收到了新的啟動請求(在第一個請求結(jié)束時停止服務(wù)會終止第二個請求)。為了避免這一問題,您可以使用 stopSelf(int)確保服務(wù)停止請求始終基于最近的啟動請求。也就說,在調(diào)用 stopSelf(int)時,傳遞與停止請求的 ID 對應(yīng)的啟動請求的 ID(傳遞給 [onStartCommand()](http://developer.android.com/reference/android/app/Service.html#onStartCommand(android.content.Intent, int, int))的 startId) 。然后,如果在您能夠調(diào)用stopSelf(int)之前服務(wù)收到了新的啟動請求, ID 就不匹配,服務(wù)也就不會停止。
注意:為了避免浪費系統(tǒng)資源和消耗電池電量,應(yīng)用必須在工作完成之后停止其服務(wù)。 如有必要,其他組件可以通過調(diào)用 stopService()來停止服務(wù)。即使為服務(wù)啟用了綁定,一旦服務(wù)收到對 [onStartCommand()](http://developer.android.com/reference/android/app/Service.html#onStartCommand(android.content.Intent, int, int))的調(diào)用,您始終仍須親自停止服務(wù)。
創(chuàng)建綁定服務(wù)
綁定服務(wù)允許應(yīng)用組件通過調(diào)用 [bindService()](http://developer.android.com/reference/android/content/Context.html#bindService(android.content.Intent, android.content.ServiceConnection, int))與其綁定,以便創(chuàng)建長期連接(通常不允許組件通過調(diào)用startService()來啟動它)。
如需與 Activity 和其他應(yīng)用組件中的服務(wù)進行交互,或者需要通過進程間通信 (IPC) 向其他應(yīng)用公開某些應(yīng)用功能,則應(yīng)創(chuàng)建綁定服務(wù)。
要創(chuàng)建綁定服務(wù),必須實現(xiàn) onBind()回調(diào)方法以返回 IBinder,用于定義與服務(wù)通信的接口。然后,其他應(yīng)用組件可以調(diào)用 [bindService()](http://developer.android.com/reference/android/content/Context.html#bindService(android.content.Intent, android.content.ServiceConnection, int))來檢索該接口,并開始對服務(wù)調(diào)用方法。服務(wù)只用于與其綁定的應(yīng)用組件,因此如果沒有組件綁定到服務(wù),則系統(tǒng)會銷毀服務(wù)(您不必按通過 [onStartCommand()](http://developer.android.com/reference/android/app/Service.html#onStartCommand(android.content.Intent, int, int))
啟動的服務(wù)那樣來停止綁定服務(wù))。
要創(chuàng)建綁定服務(wù),首先必須定義指定客戶端如何與服務(wù)通信的接口。 服務(wù)與客戶端之間的這個接口必須是IBinder的實現(xiàn),并且服務(wù)必須從 onBind()回調(diào)方法返回它。一旦客戶端收到 IBinder
,即可開始通過該接口與服務(wù)進行交互。
多個客戶端可以同時綁定到服務(wù)。客戶端完成與服務(wù)的交互后,會調(diào)用 unbindService()
取消綁定。一旦沒有客戶端綁定到該服務(wù),系統(tǒng)就會銷毀它。
有多種方法實現(xiàn)綁定服務(wù),其實現(xiàn)比啟動服務(wù)更為復(fù)雜,因此綁定服務(wù)將在有關(guān)綁定服務(wù)的單獨文檔中專門討論。
向用戶發(fā)送通知
一旦運行起來,服務(wù)即可使用 Toast 通知或狀態(tài)欄通知來通知用戶所發(fā)生的事件。
Toast 通知是指出現(xiàn)在當前窗口的表面、片刻隨即消失不見的消息,而狀態(tài)欄通知則在狀態(tài)欄提供內(nèi)含消息的圖標,用戶可以選擇該圖標來采取操作(例如啟動 Activity)。
通常,當某些后臺工作已經(jīng)完成(例如文件下載完成)且用戶現(xiàn)在可以對其進行操作時,狀態(tài)欄通知是最佳方法。 當用戶從展開視圖中選定通知時,通知即可啟動 Activity(例如查看已下載的文件)。
如需了解詳細信息,請參閱 Toast 通知或狀態(tài)欄通知開發(fā)者指南。
在前臺運行服務(wù)
前臺服務(wù)被認為是用戶主動意識到的一種服務(wù),因此在內(nèi)存不足時,系統(tǒng)也不會考慮將其終止。 前臺服務(wù)必須為狀態(tài)欄提供通知,狀態(tài)欄位于“正在進行”標題下方,這意味著除非服務(wù)停止或從前臺刪除,否則不能清除通知。
例如,應(yīng)該將從服務(wù)播放音樂的音樂播放器設(shè)置為在前臺運行,這是因為用戶明確意識到其操作。 狀態(tài)欄中的通知可能表示正在播放的歌曲,并允許用戶啟動 Activity 來與音樂播放器進行交互。
要請求讓服務(wù)運行于前臺,請調(diào)用 [startForeground()](http://developer.android.com/reference/android/app/Service.html#startForeground(int, android.app.Notification))。此方法取兩個參數(shù):唯一標識通知的整型數(shù)和狀態(tài)欄的 Notification。例如:
Notification notification = new Notification(R.drawable.icon, getText(R.string.ticker_text),
System.currentTimeMillis());
Intent notificationIntent = new Intent(this, ExampleActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, getText(R.string.notification_title),
getText(R.string.notification_message), pendingIntent);
startForeground(ONGOING_NOTIFICATION_ID, notification);
注意:提供給 [startForeground()](http://developer.android.com/reference/android/app/Service.html#startForeground(int, android.app.Notification))的整型 ID 不得為 0
要從前臺刪除服務(wù),請調(diào)用 stopForeground()。此方法取一個布爾值,指示是否也刪除狀態(tài)欄通知。 此方法絕對不會停止服務(wù)。
但是,如果您在服務(wù)正在前臺運行時將其停止,則通知也會被刪除。
如需了解有關(guān)通知的詳細信息,請參閱創(chuàng)建狀態(tài)欄通知。
管理服務(wù)生命周期
服務(wù)的生命周期比 Activity 的生命周期要簡單得多。但是,密切關(guān)注如何創(chuàng)建和銷毀服務(wù)反而更加重要,因為服務(wù)可以在用戶沒有意識到的情況下運行于后臺。
服務(wù)生命周期(從創(chuàng)建到銷毀)可以遵循兩條不同的路徑:
啟動服務(wù)
該服務(wù)在其他組件調(diào)用 startService()時創(chuàng)建,然后無限期運行,且必須通過調(diào)用 stopSelf()來自行停止運行。此外,其他組件也可以通過調(diào)用 stopService()來停止服務(wù)。服務(wù)停止后,系統(tǒng)會將其銷毀。綁定服務(wù)
該服務(wù)在另一個組件(客戶端)調(diào)用 [bindService()](http://developer.android.com/reference/android/content/Context.html#bindService(android.content.Intent, android.content.ServiceConnection, int))時創(chuàng)建。然后,客戶端通過 IBinder接口與服務(wù)進行通信??蛻舳丝梢酝ㄟ^調(diào)用 unbindService()關(guān)閉連接。多個客戶端可以綁定到相同服務(wù),而且當所有綁定全部取消后,系統(tǒng)即會銷毀該服務(wù)。 (服務(wù)不必自行停止運行。)
這兩條路徑并非完全獨立。也就是說,您可以綁定到已經(jīng)使用 startService()啟動的服務(wù)。例如,可以通過使用 Intent(標識要播放的音樂)調(diào)用 startService()來啟動后臺音樂服務(wù)。隨后,可能在用戶需要稍加控制播放器或獲取有關(guān)當前播放歌曲的信息時,Activity 可以通過調(diào)用 [bindService()](http://developer.android.com/reference/android/content/Context.html#bindService(android.content.Intent, android.content.ServiceConnection, int))綁定到服務(wù)。在這種情況下,除非所有客戶端均取消綁定,否則 stopService() 或 stopSelf()不會真正停止服務(wù)。
實現(xiàn)生命周期回調(diào)
與 Activity 類似,服務(wù)也擁有生命周期回調(diào)方法,您可以實現(xiàn)這些方法來監(jiān)控服務(wù)狀態(tài)的變化并適時執(zhí)行工作。 以下框架服務(wù)展示了每種生命周期方法:
public class ExampleService extends Service {
int mStartMode; // indicates how to behave if the service is killed
IBinder mBinder; // interface for clients that bind
boolean mAllowRebind; // indicates whether onRebind should be used
@Override
public void onCreate() {
// The service is being created
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// The service is starting, due to a call to startService()
return mStartMode;
}
@Override
public IBinder onBind(Intent intent) {
// A client is binding to the service with bindService()
return mBinder;
}
@Override
public boolean onUnbind(Intent intent) {
// All clients have unbound with unbindService()
return mAllowRebind;
}
@Override
public void onRebind(Intent intent) {
// A client is binding to the service with bindService(),
// after onUnbind() has already been called
}
@Override
public void onDestroy() {
// The service is no longer used and is being destroyed
}
}
注:與 Activity 生命周期回調(diào)方法不同,您不需要調(diào)用這些回調(diào)方法的超類實現(xiàn)。

服務(wù)生命周期左圖顯示了使用 startService()所創(chuàng)建的服務(wù)的生命周期,右圖顯示了使用 [bindService()](http://developer.android.com/reference/android/content/Context.html#bindService(android.content.Intent, android.content.ServiceConnection, int))所創(chuàng)建的服務(wù)的生命周期。
通過實現(xiàn)這些方法,您可以監(jiān)控服務(wù)生命周期的兩個嵌套循環(huán):
- 服務(wù)的 整個生命周期 從調(diào)用 onCreate()開始起,到 onDestroy()返回時結(jié)束。與 Activity 類似,服務(wù)也在 onCreate()中完成初始設(shè)置,并在 onDestroy()中釋放所有剩余資源。例如,音樂播放服務(wù)可以在onCreate()中創(chuàng)建用于播放音樂的線程,然后在 onDestroy()中停止該線程。無論服務(wù)是通過 startService()還是 [bindService()](http://developer.android.com/reference/android/content/Context.html#bindService(android.content.Intent, android.content.ServiceConnection, int))創(chuàng)建,都會為所有服務(wù)調(diào)用 onCreate()和onDestroy()方法。
- 服務(wù)的 有效生命周期 從調(diào)用 [onStartCommand()](http://developer.android.com/reference/android/app/Service.html#onStartCommand(android.content.Intent, int, int))或 onBind()方法開始。每種方法均有 Intent對象,該對象分別傳遞到 startService()或 [bindService()](http://developer.android.com/reference/android/content/Context.html#bindService(android.content.Intent, android.content.ServiceConnection, int))。對于啟動服務(wù),有效生命周期與整個生命周期同時結(jié)束(即便是在 [onStartCommand()](http://developer.android.com/reference/android/app/Service.html#onStartCommand(android.content.Intent, int, int))返回之后,服務(wù)仍然處于活動狀態(tài))。對于綁定服務(wù),有效生命周期在 onUnbind()返回時結(jié)束
注:盡管啟動服務(wù)是通過調(diào)用 stopSelf()或 stopService()來停止,但是該服務(wù)并無相應(yīng)的回調(diào)(沒有onStop()回調(diào))。因此,除非服務(wù)綁定到客戶端,否則在服務(wù)停止時,系統(tǒng)會將其銷毀—onDestroy()是接收到的唯一回調(diào)。
上圖說明了服務(wù)的典型回調(diào)方法。盡管該圖分開介紹通過 startService()創(chuàng)建的服務(wù)和通過[bindService()](http://developer.android.com/reference/android/content/Context.html#bindService(android.content.Intent, android.content.ServiceConnection, int))創(chuàng)建的服務(wù),但是請記住,不管啟動方式如何,任何服務(wù)均有可能允許客戶端與其綁定。因此,最初用 [onStartCommand()](http://developer.android.com/reference/android/app/Service.html#onStartCommand(android.content.Intent, int, int))(通過客戶端調(diào)用 startService())啟動的服務(wù)仍可接收對 onBind()的調(diào)用(當客戶端調(diào)用 [bindService()](http://developer.android.com/reference/android/content/Context.html#bindService(android.content.Intent, android.content.ServiceConnection, int))時)。
如需了解有關(guān)創(chuàng)建提供綁定的服務(wù)的詳細信息,請參閱綁定服務(wù)文檔,該文檔的管理綁定服務(wù)的生命周期部分提供了有關(guān) onRebind()回調(diào)方法的更多信息。