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的子類如果直接的函數調用接口。

[java]
// 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; 
 } 

// 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方法定義:

[java] 
@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(); 
@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 的實現:

[java]
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(); 
 } 
}; 
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之間的綁定:

[java] 
bindService(new Intent(Binding.this, 
 LocalService.class), mConnection, Context.BIND_AUTO_CREATE); 
bindService(new Intent(Binding.this,
 LocalService.class), mConnection, Context.BIND_AUTO_CREATE);
如果需要斷開與Service之間的綁定,則調用unbindService.

[java] www.aiwalls.com
unbindService(mConnection); 
unbindService(mConnection);[java] view plaincopyprint?
<p>如果你熟悉Socket編程, Client 綁定”Bound”Service 的方法和使用Socket通信非常類似。</p><p><a href=/wp-content/images1/20181012/20120630034121506108.png"><img class="aligncenter size-full wp-image-1254" title="20110514001" alt="" src=/wp-content/images1/20181012/20120630034121506108.png" width="339" height="501" /></a></p> 
<p>如果你熟悉Socket編程, Client 綁定”Bound”Service 的方法和使用Socket通信非常類似。</p><p><a href=/wp-content/images1/20181012/20120630034121506108.png"><img class="aligncenter size-full wp-image-1254" title="20110514001" alt="" src=/wp-content/images1/20181012/20120630034121506108.png" width="339" height="501" /></a></p>

作者:mapdigit
 

 

發佈留言