Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android系統教程 >> Android開發教程 >> Android ApiDemos示例解析(39):App->Service->Local Service Binding

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

編輯:Android開發教程

本例和下列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();
}
};

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved