一、IBinder機制
IBinder機制是android系統中非常重要的一部分,他常常被用來進行進程間通信。IBinder強調了一組協議,通常通過socket or binder在進程之間共享。IBinder也可以用來實現遠程調用(RPC)。
二、IBinder傳遞哪些
IBinder提供給調用者單獨使用的只有客戶端的代理(proxy)接口,而真正執行客戶端操作的對象是在服務端中,由ServiceManager進行管理的.這個代理接口的作用就是通過它包裝的IBinder 代理對象使客戶端和服務端相互通信.在這個過程中,客戶端需要發起一個遠程函數調用請求,並且為這個請求提供調用函數消息以及所需要的參數信息,向服務端傳遞它需要執行的操作,然後服務端根據客戶端提供的消息以及參數信息,執行相應的函數。之後服務器返迴響應消息,代理接口將響應消息傳遞給客戶端,客戶端解包消息後,若有返回值,則完成了一次RPC調用過程。
三、IBinder對象
IBinder對象是一種系統級別的進程間通信機制。它提供了一組接口,使一個進程可以通過接口與其他進程共享數據。這些數據可以是簡單的數值對象,也可以是更複雜的數據結構。
在一個android應用程序中,IBinder作為進程間通信的最基本實現,理解IBinder對象在android應用程序中的應用實現機制是很重要的。
四、IBinder傳遞哪些數據
IBinder接口是android提供的進程間通信的基礎接口,它允許一個進程將自己的IBinder對象傳遞給另一個進程。任何一個實現了這個接口的類都可以作為一個IBinder對象。IBinder中傳遞的數據協議和RMI差不多。大部分基本數據類型和一些java對象實例都可以在不同進程間傳遞。當傳遞非基本類型或自定義類型時,這個非基本類型或自定義類型的對象必須實現Parcelable接口,以便將其數據打包並在進程間傳遞。
五、IBinder Stub
IBinderStub是一個AIDL文件中生成的Java code。它是一個代理者,它的存在在於接收來自Client的請求信息,並將請求轉發到Service端處理。
AIDL 文件中,每一個接口會自動產生一個以“Stub”為後綴的接口實現類。這個Stub繼承了Binder類並實現了要實現的接口,同時將方法封裝上一層,用於處理Service端的調用以及Client的請求。 。Stub類將會是在Client端調用的,它必須將要請求的操作以及請求數據打包成一個Parcel發送到Service端,服務端響應後再將結果封裝成一個Parcel再通過Binder返回到客戶端。
六、IBinder的含義
說白了,IBinder就是一個遠程過程調用(RPC)的接口,它總是伴隨着計算進程,相對於計算進程是穩定的(當連接失效時會得到通知)。Client編程使用IBinder不需要關心 Server副本的實際位置等細節,而只需要對通用接口按照固定的函數簽名進行調用。IBinder通常隱含地使用unix domain sockets,也可以通過Binder Driver實現的大規模系統,它是一種IPC(進程間通信)機制,要讓自己實現IBinder接口,我們必須理解它的原理。
七、IBinder和Binder的區別
Binder和IBinder的區別在於Binder是個類,而IBinder是Binder的一個接口。Binder繼承自IBinder,並對IBinder進行擴展,增加了更多的功能。所以,我們可以這樣理解:IBinder是Binder的一部分,IBinder按照Binder進行了一個規定的接口描述。
八、IBinder.false
IBinder.FALSE通常用於返回值,表示該屬性未定義或未初始化。IBinder.FALSE為系統所許可,我們在使用IBinder時,需要清楚其具體含義。
九、Binder軟件
Binder是為android系統設計的一個輕量級的IPC(進程間通信)機制。它是一個Linux內核的設備驅動程序,用於其他系統組件之間的通信。Binder的核心組件負責驅動進程間通信,在這些組件之間傳遞RPC請求和響應消息。
十、Binder是什麼意思
Binder是是Android中提供的一種IPC(進程間通信)基礎服務,採用客戶端-服務端的模式,通過IBinder接口實現進程之間的通信。一般而言,Android中採用Binder進行IPC操作。
/** * Helper class for implementing an IBinder that can receive calls. */ public static abstract class Stub extends Binder implements IInterface { private static final String DESCRIPTOR = "android.os.IBinder"; /** * Construct the stub at attach it to the interface. */ public Stub() { attachInterface(this, DESCRIPTOR); } /** * Cast an IBinder object into an your interface extension, generating a * proxy if needed. */ public static IInterface asInterface(IBinder obj) { if (obj == null) { return null; } IInterface iin = obj.queryLocalInterface(DESCRIPTOR); if (iin != null && iin instanceof IInterface) { return (IInterface) iin; } return new Proxy(obj); } /** * Return the Binder object associated with this interface, creating it * if needed. */ public IBinder asBinder() { return this; } /** * Implement your interface methods here. */ public abstract void someFunction_defined_in_your_interface() throws RemoteException; /** * Called by android.os.BinderProxy.finalize(). */ @Override protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException { switch (code){ case TRANSACTION_someFunction_defined_in_your_interface:{ // Input data is at "data", output data is at "reply". data.enforceInterface(DESCRIPTOR); // Ensure correct interface. someFunction_defined_in_your_interface(); reply.writeNoException(); return true; } default: return super.onTransact(code, data, reply, flags); } } /** * A proxy that calls the actual Binder implementation. */ private static class Proxy implements IInterface { private IBinder mRemote; public Proxy(IBinder remote) { mRemote = remote; } public java.lang.String getInterfaceDescriptor() { return DESCRIPTOR; } public IBinder asBinder() { return mRemote; } // Define your wrapper methods that calls through the Binder proxy. public void someFunction_defined_in_your_interface() throws RemoteException { // Create a new Parcel and data to send to the Binder. Parcel data = Parcel.obtain(); Parcel reply = Parcel.obtain(); data.writeInterfaceToken(DESCRIPTOR); // Ensure correct interface. mRemote.transact(Stub.TRANSACTION_someFunction_defined_in_your_interface, data, reply, 0); reply.readException(); reply.recycle(); data.recycle(); } } }
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/257090.html