您的位置:

深入了解IBinder

一、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();
            }
        }
    }