关于android:Binder机制应用篇

3次阅读

共计 4832 个字符,预计需要花费 13 分钟才能阅读完成。

Binder 机制 在 Android 中的具体实现原理

1. 实现步骤

Binder机制在 Android中的实现次要依附 Binder类,其实现了 IBinder 接口

  • 实例阐明:即:Client过程 须要调用Server 过程的加法函数(将整数 a 和 b 相加)
  1. Client过程 须要传两个整数给 Server过程
  2. Server过程 须要把相加后的后果 返回给 Client过程
  • 具体步骤 上面,我会依据 Binder 跨过程通信机制 模型的步骤进行剖析

✔步骤 1:注册服务

  • 过程形容 Server 过程 通过 Binder 驱动 向Service Manager 过程注册服务
  • 代码实现 Server 过程创立 一个 Binder对象

      1. Binder实体是 Server 过程 在Binder 驱动中的存在模式
      1. 该对象保留 ServerService Manager 的信息(保留在内核空间中)
      1. Binder驱动通过 内核空间的 Binder实体 找到用户空间的 Server 对象
  • 代码剖析
    Binder binder = new Stub();
    // 步骤 1:创立 Binder 对象 ->> 剖析 1
    // 步骤 2:创立 IInterface 接口类 的匿名类
    // 创立前,须要事后定义 继承了 IInterface 接口的接口 --> 剖析 3
    IInterface plus = new IPlus(){
        // 确定 Client 过程须要调用的办法
        public int add(int a,int b){return a+b;}
        // 实现 IInterface 接口中惟一的办法
        public IBinder asBinder(){return null ;}
    };
    // 步骤 3
    binder.attachInterface(plus,"add two int");
    // 1. 将(add two int,plus)作为(key,value)对存入到 Binder 对象中的一个 Map<String,IInterface> 对象中
    // 2. 之后,Binder 对象 可依据 add two int 通过 queryLocalIInterface()取得对应 IInterface 对象(即 plus)的援用,可依附该援用实现对申请办法的调用
    // 剖析结束,跳出
    
    <-- 剖析 1:Stub 类 -->
    public class Stub extends Binder {
    // 继承自 Binder 类 ->> 剖析 2
    // 复写 onTransact()@Override
    boolean onTransact(int code, Parcel data, Parcel reply, int flags){
        // 具体逻辑等到步骤 3 再具体解说,此处先跳过
        switch (code) {
            case Stub.add:{data.enforceInterface("add two int");
                int arg0 = data.readInt();
                int arg1 = data.readInt();
                int result = this.queryLocalIInterface("add two int") .add(arg0, arg1);
                reply.writeInt(result);
                return true;
            }
        }
        return super.onTransact(code, data, reply, flags);
    }
    
    // 回到下面的步骤 1,持续看步骤 2
    
    <-- 剖析 2:Binder 类 -->
    public class Binder implement IBinder{
        // Binder 机制在 Android 中的实现次要依附的是 Binder 类,其实现了 IBinder 接口 // IBinder 接口:// 定义了近程操作对象的根本接口,代表了一种跨过程传输的能力
        // 零碎会为每个实现了 IBinder 接口的对象提供跨过程传输能力
        // 即 Binder 类对象具备了跨过程传输的能力
        
        void attachInterface(IInterface plus, String descriptor);// 作用:// 1. 将(descriptor,plus)作为(key,value)对存入到 Binder 对象中的一个 Map<String,IInterface> 对象中
        // 2. 之后,Binder 对象 可依据 descriptor 通过 queryLocalIInterface()取得对应 IInterface 对象(即 plus)的援用,// 可依附该援用实现对申请办法的调用
        
        IInterface queryLocalInterface(Stringdescriptor);// 作用:依据 参数 descriptor 查找相应的 IInterface 对象(即 plus 援用)boolean onTransact(int code, Parcel data, Parcel reply, int flags);// 定义:继承自 IBinder 接口的
        // 作用:执行 Client 过程所申请的指标办法(子类须要复写)// 参数阐明:// code:Client 过程申请办法标识符。即 Server 过程依据该标识确定所申请的指标办法
        // data:指标办法的参数。(Client 过程传进来的,此处就是整数 a 和 b)// reply:指标办法执行后的后果(返回给 Client 过程)// 注:运行在 Server 过程的 Binder 线程池中;当 Client 过程发动近程申请时,近程申请会要求零碎底层执行回调该办法
        
        
        final class BinderProxy implements IBinder {
            // 即 Server 过程创立的 Binder 对象的代理对象类
            // 该类属于 Binder 的外部类
        }
        
        // 回到剖析 1 原处
        
    }
    
    <-- 剖析 3:IInterface 接口实现类 -->
    public interface IPlus extends IInterface {
        // 继承自 IInterface 接口 ->> 剖析 4
        // 定义须要实现的接口办法,即 Client 过程须要调用的办法
        public int add(int a,int b);
    // 返回步骤 2
    }
    
    <-- 剖析 4:IInterface 接口类 -->
    // 过程间通信定义的通用接口
    // 通过定义接口,而后再服务端实现接口、客户端调用接口,就可实现跨过程通信。public interface IInterface{
        // 只有一个办法:返回以后接口关联的 Binder 对象。public IBinder asBinder();}
    // 回到剖析 3 原处
    
    

注册服务后,Binder驱动持有 Server过程创立的 Binder实体

✔步骤 2:获取服务(Client)

  • Client 过程 应用 某个 service 前(此处是 相加函数 ),须通过Binder 驱动向 ServiceManager 过程 获取相应的 Service 信息
  • 具体代码实现过程如下:

此时,Client过程与Server 过程曾经建设了连贯

✔步骤 3:应用服务(Client)

Client 过程 依据获取到的 service信息(Binder 代理对象),通过 Binder 驱动建设与该 Service 所在Server 过程通信的链路,并开始应用服务

  • 过程形容

    1. Client过程通信的链路,并开始应用服务 过程 将参数(整数 a 和 b)发送到Server 过程
    2. Server过程 依据Client 过程要求调用 指标办法(即加法函数)
    3. Server过程 将指标办法的后果(即加法后的后果)返回给 Client 过程
  • 代码实现过程

    3.1:Client过程 将参数(整数 a 和 b)发送到 Server过程

        // 1. Client 过程 将须要传送的数据写入到 Parcel 对象中
    // data = 数据 = 指标办法的参数(Client 过程传进来的,此处就是整数 a 和 b)+ IInterface 接口对象的标识符 descriptor
    android.os.Parcel data = android.os.Parcel.obtain();
    data.writeInt(a);
    data.writeInt(b);
    
    data.writeInterfaceToken("add two int");    
    // 办法对象标识符让 Server 过程在 Binder 对象中依据 "add two int" 通过 queryLocalIInterface()// 查找相应的 IInterface 对象(即 Server 创立的 plus),Client 过程须要调 用的相加办法就在该对象中
    
    android.os.Parcel reply = android.os.Parcel.obtain();
    // reply:指标办法执行后的后果(此处是相加后的后果)// 2. 通过 调用代理对象的 transact()将 上述数据发送到 Binder 驱动
    binderproxy.transact(Stub.add, data, reply, 0)
    // 参数阐明:// 1. Stub.add:指标办法的标识符(Client 过程 和 Server 过程 本身约定,可为任意)// 2. data:上述的 Parcel 对象
    // 3. reply:返回后果
    // 0:可不论
    
    
    // 注:在发送数据后,Client 过程的该线程会临时被挂起
    // 所以,若 Server 过程执行的耗时操作,请不要应用主线程,以避免 ANR
    
    
    
    // 3. Binder 驱动依据 代理对象 找到对应的真身 Binder 对象所在的 Server 过程(零碎主动执行)// 4. Binder 驱动把 数据 发送到 Server 过程中,并告诉 Server 过程执行解包(零碎主动执行)123456789101112131415161718192021222324252627
      

3.2:Server 过程依据 Client 过程要求 调用 指标办法(即加法函数)

    // 1. 收到 Binder 驱动告诉后,Server 过程通过回调 Binder 对象 onTransact()进行数据解包 & 调用指标办法
  public class Stub extends Binder {
      // 复写 onTransact()@Override
      boolean onTransact(int code, Parcel data, Parcel reply, int flags){
          // code 即在 transact()中约定的指标办法的标识符
          switch (code) {
              case Stub.add:{
              
                  // a. 解包 Parcel 中的数据
                  data.enforceInterface("add two int");
                  // a1. 解析指标办法对象的标识符
                  int arg0 = data.readInt();
                  int arg1 = data.readInt();
                  // a2. 取得指标办法的参数
                  
                  // b. 依据 "add two int" 通过 queryLocalIInterface()获取相应的 IInterface 对象
                  //(即 Server 创立的 plus)的援用,通过该对象援用调用办法
                  int result = this.queryLocalIInterface("add two int") .add(arg0, arg1);
                  
                  // c. 将计算结果写入到 reply
                  reply.writeInt(result);
                  return true;
              }
          }
      return super.onTransact(code, data, reply, flags);
      // 2. 将结算后果返回 到 Binder 驱动
             

3.3:过程 将指标办法的后果(即加法后的后果)返回给过程

           // 2. 将结算后果返回 到 Binder 驱动    
           // 1. Binder 驱动依据 代理对象 沿原路 将后果返回 并告诉 Client 过程获取返回后果
           // 2. 通过代理对象 接管后果(之前被挂起的线程被唤醒)binderproxy.transact(Stub.ADD, data, reply, 0);reply.readException();;result = reply.readInt();}
}

12345678

2.原理图 & 流程图

  • 总结 上面,我用一个 原理图 & 流程图 来总结步骤 3 的内容

3. 长处

比照 LinuxAndroid 基于 Linux)上的其余过程通信形式(管道、音讯队列、共享内存、信号量、Socket),Binder 机制的长处有:

4. 总结

本文次要具体解说 跨过程通信模型 Binder 机制,总结如下:

特地地,对于从模型构造组成的 Binder 驱动来说:

  • 整个 Binder模型的原理步骤 & 源码剖析

正文完
 0