天天看點

Android ApiDemos示例解析(39):App->Service->Local Service Binding

本例和下列Local Service Controller 的Activity代碼都定義在LocalServiceActivities.Java 中,作為LocalServiceActivities 内部類實作的。 調用的Service為LocalService。

LocalService既可以做為“Started” Service,也可以做為”Bound” Service。

一個“Bound” Service 可以通過Client/Service模式提供Service。它運作應用程式元件(比如Activity)與之綁定,然後接受請求并傳回響應或者提供程序間通信機制,它的生命周期通常與調用它的元件(如Activity)相同。 而對于LocalService即作為“Started” Service又作為“Bound”Service,如果LocalService已作為“Started” Service啟動,中即使所有Client都斷開與它的綁定,LocalService也不會停止。

如果一個Service需要作為“Bound”Service運作其它元件與之綁定,就必須實作onBind方法。這個方法傳回一個IBound對象給Client。Client可以通過這個IBind對象來調用Service的方法。

Client可以通過bindService來實作與“Bound”Service的綁定,Service 與 Client 的綁定是異步實作的,是以Client 需要通過 ServiceConnection 接口來監視與Service之間的連接配接。 Client 調用bindService 時,bindService 立即傳回,之後當Android系統為Client 和Service之間建立起連結後調用 ServiceConnection 的 onServiceConnection 來通知Client 與Service之間的連結建立成功,并給Client傳回 Service 提供的IBind對象。

LocalService 隻提供給同一個Application的元件使用,不提供程序間通信接口,是以隻需要派生一個Binder的子類如果直接的函數調用接口。

// Class for clients to access.  Because we know
 // this service always runs in the same process as
 //its clients, we don't need to deal with IPC.
public class LocalBinder extends Binder {
 LocalService getService() {
 return LocalService.this;
 }
}
           

LocalBinder隻提供了一個方法getService ,傳回LocalService 對象自身。

LocalService的 onBind方法定義:

@Override
public IBinder onBind(Intent intent) {
 return mBinder;
}
 
// This is the object that receives interactions from clients.  See
// RemoteService for a more complete example.
private final IBinder mBinder = new LocalBinder();
           

onBind的傳回值為mBinder 為 LocalBinder類對象。它定義了一個方法getService。

再來看看Client類 LocalServiceActivities.Binding 的實作:

private LocalService mBoundService;
 
private ServiceConnection mConnection = new ServiceConnection() {
 public void onServiceConnected(ComponentName className, IBinder service) {
 // This is called when the connection with the service has been
 // established, giving us the service object we can use to
 // interact with the service.  Because we have bound to a explicit
 // service that we know is running in our own process, we can
 // cast its IBinder to a concrete class and directly access it.
 mBoundService = ((LocalService.LocalBinder)service).getService();
 
 // Tell the user about this for our demo.
 Toast.makeText(Binding.this, R.string.local_service_connected,
 Toast.LENGTH_SHORT).show();
 }
 
 public void onServiceDisconnected(ComponentName className) {
 // This is called when the connection with the service has been
 // unexpectedly disconnected -- that is, its process crashed.
 // Because it is running in our same process, we should never
 // see this happen.
 mBoundService = null;
 Toast.makeText(Binding.this, R.string.local_service_disconnected,
 Toast.LENGTH_SHORT).show();
 }
};
           

mConnection定義了ServiceConnection接口,onServiceConnected ,onServiceDisconnected分别在Service與Client 之間建立連結和斷開連結時調用。其中IBinder service 就是 Service 的onBind的傳回對象。 這裡将其通過類型轉換為LocalService也就是mBoundService。

Client 通過調用bindService建立與Service之間的綁定:

bindService(new Intent(Binding.this,
 LocalService.class), mConnection, Context.BIND_AUTO_CREATE);
           

如果需要斷開與Service之間的綁定,則調用unbindService.

unbindService(mConnection);
           

如果你熟悉Socket程式設計, Client 綁定”Bound”Service 的方法和使用Socket通信非常類似。

Android ApiDemos示例解析(39):App->Service->Local Service Binding

繼續閱讀