Binder 机制AIDL 分析 ( 创建 AIDL 文件 | 创建 Parcelable 类 | AIDL 中使用 Parcelable 类 | 编译工程生成 AIDL 对应的Java源文件 )(代

Posted 韩曙亮

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Binder 机制AIDL 分析 ( 创建 AIDL 文件 | 创建 Parcelable 类 | AIDL 中使用 Parcelable 类 | 编译工程生成 AIDL 对应的Java源文件 )(代相关的知识,希望对你有一定的参考价值。





一、创建 AIDL 文件




1、创建 AIDL 目录


android Studio 工程中 , 创建 aidl 文件 ;

右键点击 main 目录 , 选择 " New / Directory " ,

选择创建 " aidl " 目录 ,

创建好的 aidl 目录如下 :


2、创建 AIDL 文件


右键点击 " aidl " 目录 , 选择 " New / AIDL / AIDL File " 文件 ;

弹出对话框 , 输入 AIDL 接口名称 , 输入完毕后 , 选择 " Finish " 选项 ;


3、创建 Parcelable 类


创建 Parcelable 类 :

package kim.hsl.aidl_demo;

import android.os.Parcel;
import android.os.Parcelable;

public class Student implements Parcelable {

    private String name;

    public Student(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    protected Student(Parcel in) {
        name = in.readString();
    }

    public static final Creator<Student> CREATOR = new Creator<Student>() {
        @Override
        public Student createFromParcel(Parcel in) {
            return new Student(in);
        }

        @Override
        public Student[] newArray(int size) {
            return new Student[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
    }

    public void readFromParcel(Parcel desc) {
        name = desc.readString();
    }

    @Override
    public String toString() {
        return "name=" + name;
    }
}

4、AIDL 目录下声明 Parcelable 类


在 aidl 目录下声明 Parcelable 类 : 在 aidl 目录下创建 Student.aidl 文件 , 然后声明如下内容 ;

package kim.hsl.aidl_demo;

parcelable Student;


5、AIDL 中使用 Parcelable 类


在创建的 AIDL 接口中使用 Student 类 :

① 首先要导入 Student 类 , import kim.hsl.aidl_demo.Student;

② 参数的输入输出 , in 写入, out 输出, inout 写入和输出 ;

// IMyAidlInterface.aidl
package kim.hsl.aidl_demo;

import kim.hsl.aidl_demo.Student;

// Declare any non-default types here with import statements

interface IMyAidlInterface {
    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
    void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
            double aDouble, String aString);

    /**
     * in 写入, out 输出, inout 写入和输出
     */
    void addStudent(inout Student student);

    /**
     * 获取 Student 集合
     */
    List<Student> getStudents();
}





二、编译工程生成 AIDL 文件对应的 Java 源文件




1、编译工程


点击 " 菜单栏 / Build / Make Project " 选项 , 即可编译当前的工程 , 进而生成 AIDL 接口对应的 Java 源文件 ;

编译后 , 在 " AIDL_Demo\\app\\build\\generated\\aidl_source_output_dir\\debug\\out\\kim\\hsl\\aidl_demo " 目录 , 生成了 AIDL 文件对应的源码 :


2、生成的 AIDL 对应 Java 源文件


下面的源码是编译生成的 Java 源文件 :

/*
 * This file is auto-generated.  DO NOT MODIFY.
 */
package kim.hsl.aidl_demo;
// Declare any non-default types here with import statements

public interface IMyAidlInterface extends android.os.IInterface
{
  /** Default implementation for IMyAidlInterface. */
  public static class Default implements kim.hsl.aidl_demo.IMyAidlInterface
  {
    /**
         * Demonstrates some basic types that you can use as parameters
         * and return values in AIDL.
         */
    @Override public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, java.lang.String aString) throws android.os.RemoteException
    {
    }
    /**
         * in 写入, out 输出, inout 写入和输出
         */
    @Override public void addStudent(kim.hsl.aidl_demo.Student student) throws android.os.RemoteException
    {
    }
    /**
         * 获取 Student 集合
         */
    @Override public java.util.List<kim.hsl.aidl_demo.Student> getStudents() throws android.os.RemoteException
    {
      return null;
    }
    @Override
    public android.os.IBinder asBinder() {
      return null;
    }
  }
  /** Local-side IPC implementation stub class. */
  public static abstract class Stub extends android.os.Binder implements kim.hsl.aidl_demo.IMyAidlInterface
  {
    private static final java.lang.String DESCRIPTOR = "kim.hsl.aidl_demo.IMyAidlInterface";
    /** Construct the stub at attach it to the interface. */
    public Stub()
    {
      this.attachInterface(this, DESCRIPTOR);
    }
    /**
     * Cast an IBinder object into an kim.hsl.aidl_demo.IMyAidlInterface interface,
     * generating a proxy if needed.
     */
    public static kim.hsl.aidl_demo.IMyAidlInterface asInterface(android.os.IBinder obj)
    {
      if ((obj==null)) {
        return null;
      }
      android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
      if (((iin!=null)&&(iin instanceof kim.hsl.aidl_demo.IMyAidlInterface))) {
        return ((kim.hsl.aidl_demo.IMyAidlInterface)iin);
      }
      return new kim.hsl.aidl_demo.IMyAidlInterface.Stub.Proxy(obj);
    }
    @Override public android.os.IBinder asBinder()
    {
      return this;
    }
    @Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException
    {
      java.lang.String descriptor = DESCRIPTOR;
      switch (code)
      {
        case INTERFACE_TRANSACTION:
        {
          reply.writeString(descriptor);
          return true;
        }
        case TRANSACTION_basicTypes:
        {
          data.enforceInterface(descriptor);
          int _arg0;
          _arg0 = data.readInt();
          long _arg1;
          _arg1 = data.readLong();
          boolean _arg2;
          _arg2 = (0!=data.readInt());
          float _arg3;
          _arg3 = data.readFloat();
          double _arg4;
          _arg4 = data.readDouble();
          java.lang.String _arg5;
          _arg5 = data.readString();
          this.basicTypes(_arg0, _arg1, _arg2, _arg3, _arg4, _arg5);
          reply.writeNoException();
          return true;
        }
        case TRANSACTION_addStudent:
        {
          data.enforceInterface(descriptor);
          kim.hsl.aidl_demo.Student _arg0;
          if ((0!=data.readInt())) {
            _arg0 = kim.hsl.aidl_demo.Student.CREATOR.createFromParcel(data);
          }
          else {
            _arg0 = null;
          }
          this.addStudent(_arg0);
          reply.writeNoException();
          if ((_arg0!=null)) {
            reply.writeInt(1);
            _arg0.writeToParcel(reply, android.os.Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
          }
          else {
            reply.writeInt(0);
          }
          return true;
        }
        case TRANSACTION_getStudents:
        {
          data.enforceInterface(descriptor);
          java.util.List<kim.hsl.aidl_demo.Student> _result = this.getStudents();
          reply.writeNoException();
          reply.writeTypedList(_result);
          return true;
        }
        default:
        {
          return super.onTransact(code, data, reply, flags);
        }
      }
    }
    private static class Proxy implements kim.hsl.aidl_demo.IMyAidlInterface
    {
      private android.os.IBinder mRemote;
      Proxy(android.os.IBinder remote)
      {
        mRemote = remote;
      }
      @Override public android.os.IBinder asBinder()
      {
        return mRemote;
      }
      public java.lang.String getInterfaceDescriptor()
      {
        return DESCRIPTOR;
      }
      /**
           * Demonstrates some basic types that you can use as parameters
           * and return values in AIDL.
           */
      @Override public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, java.lang.String aString) throws android.os.RemoteException
      {
        android.os.Parcel _data = android.os.Parcel.obtain();
        android.os.Parcel _reply = android.os.Parcel.obtain();
        try {
          _data.writeInterfaceToken(DESCRIPTOR);
          _data.writeInt(anInt);
          _data.writeLong(aLong);
          _data.writeInt(((aBoolean)?(1):(0)));
          _data.writeFloat(aFloat);
          _data.writeDouble(aDouble);
          _data.writeString(aString);
          boolean _status = mRemote.transact(Stub.TRANSACTION_basicTypes, _data, _reply, 0);
          if (!_status && getDefaultImpl() != null) {
            getDefaultImpl().basicTypes(anInt, aLong, aBoolean, aFloat, aDouble, aString);
            return;
          }
          _reply.readException();
        }
        finally {
          _reply.recycle();
          _data.recycle();
        }
      }
      /**
           * in 写入, out 输出, inout 写入和输出
           */
      @Override public void addStudent(kim.hsl.aidl_demo.Student student) throws android.os.RemoteException
      {
        android.os.Parcel _data = android.os.Parcel.obtain();
        android.os.Parcel _reply = android.os.Parcel.obtain();
        try {
          _data.writeInterfaceToken(DESCRIPTOR);
          if ((student!=null)) {
            _data.writeInt(1);
            student.writeToParcel(_data, 0);
          }
          else {
            _data.writeInt(0);
          }
          boolean _status = mRemote.transact(Stub.TRANSACTION_addStudent, _data, _reply, 0);
          if (!_status && getDefaultImpl() != null) {
            getDefaultImpl().addStudent(student);
            return;
          }
          _reply.readException();
          if ((0!=_reply.readInt())) {
            student.readFromParcel(_reply);
          }
        }
        finally {
          _reply.recycle();
          _data.recycle();
        }
      }
      /**
           * 获取 Student 集合
           */
      @Override public java.util.List<kim.hsl.aidl_demo.Student> getStudents() throws android.os.RemoteException
      {
        android.os.Parcel _data = android.os.Parcel.obtain();
        android.os.Parcel _reply = android.os.Parcel.obtain();
        java.util.List<kim.hsl.aidl_demo.Student> _result;
        try {
          _data.writeInterfaceToken(DESCRIPTOR);
          boolean _status = mRemote.transact(Stub.TRANSACTION_getStudents, _data, _reply, 0);
          if (!_status && getDefaultImpl() != null) {
            return getDefaultImpl().getStudents();
          }
          _reply.readException();
          _result = _reply.createTypedArrayList(kim.hsl.aidl_demo.Student.CREATOR);
        }
        finally {
          _reply.recycle();
          _data.recycle();
        }
        return _result;
      }
      public static kim.hsl.aidl_demo.IMyAidlInterface sDefaultImpl;
    }
    static final int TRANSACTION_basicTypes = (android.os.IBinderBinder 机制AIDL 分析 ( 创建 AIDL 文件 | 创建 Parcelable 类 | AIDL 中使用 Parcelable 类 | 编译工程生成 AIDL 对应的Java源文件 )(代

Binder 机制AIDL 分析 ( AIDL 通信完整流程梳理 )

Binder机制在AIDL中的实现分析

Binder机制在AIDL中的实现分析

Binder机制在AIDL中的实现分析

Binder的工作机制浅析