2009年7月27日 星期一

探討android.os.Binder

Binder物件的概觀:

他是個remotable object class, 用於行程之間的遠端溝通(IPC)主要功能定義於IBinder, 功能是一個lightweight remote procudure機制. 我們可以直接繼承Binder去實現我們的RPC protocol, 亦可以直接宣告一個Binder object來使用. 這樣說可能很攏統, 通常都在實現一個Service常會用到.

一般會在Service 實現以下程式碼:

private final IBinder mBinder = new Binder() {

@Override

protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws android.os.RemoteException {

return super.onTransact(code, data, reply, flags); }

};

在一般Activity下面實做:

Parcel data = Parcel.obtain();

Parcel reply = Parcel.obtain();
try{ mServiceBinder.transact(25, data, reply, 0}catch (RemoteException E) {}

沒有留言:

張貼留言