本例和下列Local Service Controller 的Activity代碼都定義在LocalServiceActivities.Java 中,做爲LocalServiceActivities 內部類實現的。 調用的Service爲LocalService。編程
LocalService既能夠作爲「Started」 Service,也能夠作爲」Bound」 Service。app
一個「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的方法。ide
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的子類若是直接的函數調用接口。this
// 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 對象自身。spa
LocalService的 onBind方法定義:code
@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。component
再來看看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之間的綁定:
void doBindService() { // Establish a connection with the service. We use an explicit // class name because we want a specific service implementation that // we know will be running in our own process (and thus won't be // supporting component replacement by other applications). bindService(new Intent(Binding.this, LocalService.class), mConnection, Context.BIND_AUTO_CREATE); mIsBound = true; }
若是須要斷開與Service之間的綁定,則調用unbindService.
void doUnbindService() { if (mIsBound) { // Detach our existing connection. unbindService(mConnection); mIsBound = false; } }
若是你熟悉Socket編程, Client 綁定」Bound」Service 的方法和使用Socket通訊很是相似。