AIDL之Android开发艺术探索小结

AIDL

代码示例

代码和Android开发艺术探索一致,但是这里需要注意工程目录


  • Book.java

Book.java是一个表示图书信息的类,实现了Parcelable接口

package com.joy.ipctest.aidl;

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

/**
 * Created by phomel on 2017/2/8.
 */

public class Book implements Parcelable {
    public int bookId;
    public String bookName;

    public Book(int bookId, String bookName) {
        this.bookId = bookId;
        this.bookName = bookName;
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(this.bookId);
        dest.writeString(this.bookName);
    }

    protected Book(Parcel in) {
        this.bookId = in.readInt();
        this.bookName = in.readString();
    }

    public static final Parcelable.Creator<Book> CREATOR = new Parcelable.Creator<Book>() {
        @Override
        public Book createFromParcel(Parcel source) {
            return new Book(source);
        }

        @Override
        public Book[] newArray(int size) {
            return new Book[size];
        }
    };
}
  • Book.aidl
    Book.aidl是Book类再AIDL中的声明
package com.joy.ipctest.aidl;

parcelable Book;
  • IBookManager.aidl
    IBookManager.aidl是自定义的一个接口,主要有两个方法:获取图书列表和添加图书
package com.joy.ipctest.aidl;
import com.joy.ipctest.aidl.Book;
// Declare any non-default types here with import statements

interface IBookManager {
    List<com.joy.ipctest.aidl.Book> getBookList();
    void addBook(in com.joy.ipctest.aidl.Book book);
}
  • 工程目录

这里写图片描述


建议代码码完先编译再运行,报错先不管

  • Book.aidl无法创建,Book类已存在,同名无法创建

    先随便用一个名字创建aidl文件,这样在java包的统计目录下就会出现新目录aidl,之后再改名就没问题了

  • Book.java类找不到

    IBookManager.aidl导包import com.joy.ipctest.aidl.Book;

  • Execution failed for task ‘:compileDebugAidl’

    build Tools版本和compileSdkVersion的版本不一致,在build.grade中修改,可能性较小


运行成功后不报错可以在以下目录找到生成的Binder类

  这里写图片描述

  • IBookManager.java
public interface IBookManager extends android.os.IInterface {
    /**
     * Local-side IPC implementation stub class.
     */
    public static abstract class Stub extends android.os.Binder implements com.joy.ipctest.aidl.IBookManager {

        static final int TRANSACTION_getBookList = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
        static final int TRANSACTION_addBook = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
        private static final java.lang.String DESCRIPTOR = "com.joy.ipctest.aidl.IBookManager";

        /**
         * Construct the stub at attach it to the interface.
         */
        public Stub() {
            this.attachInterface(this, DESCRIPTOR);
        }

        /**
         * Cast an IBinder object into an com.joy.ipctest.aidl.IBookManager interface,
         * generating a proxy if needed.
         */
        public static com.joy.ipctest.aidl.IBookManager asInterface(android.os.IBinder obj) {
            if ((obj == null)) {
                return null;
            }
            android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
            if (((iin != null) && (iin instanceof com.joy.ipctest.aidl.IBookManager))) {
                return ((com.joy.ipctest.aidl.IBookManager) iin);
            }
            return new com.joy.ipctest.aidl.IBookManager.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 {
            switch (code) {
                case INTERFACE_TRANSACTION: {
                    reply.writeString(DESCRIPTOR);
                    return true;
                }
                case TRANSACTION_getBookList: {
                    data.enforceInterface(DESCRIPTOR);
                    java.util.List<com.joy.ipctest.aidl.Book> _result = this.getBookList();
                    reply.writeNoException();
                    reply.writeTypedList(_result);
                    return true;
                }
                case TRANSACTION_addBook: {
                    data.enforceInterface(DESCRIPTOR);
                    com.joy.ipctest.aidl.Book _arg0;
                    if ((0 != data.readInt())) {
                        _arg0 = com.joy.ipctest.aidl.Book.CREATOR.createFromParcel(data);
                    } else {
                        _arg0 = null;
                    }
                    this.addBook(_arg0);
                    reply.writeNoException();
                    return true;
                }
            }
            return super.onTransact(code, data, reply, flags);
        }

        private static class Proxy implements com.joy.ipctest.aidl.IBookManager {
            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;
            }

            @Override
            public java.util.List<com.joy.ipctest.aidl.Book> getBookList() throws android.os.RemoteException {
                android.os.Parcel _data = android.os.Parcel.obtain();
                android.os.Parcel _reply = android.os.Parcel.obtain();
                java.util.List<com.joy.ipctest.aidl.Book> _result;
                try {
                    _data.writeInterfaceToken(DESCRIPTOR);
                    mRemote.transact(Stub.TRANSACTION_getBookList, _data, _reply, 0);
                    _reply.readException();
                    _result = _reply.createTypedArrayList(com.joy.ipctest.aidl.Book.CREATOR);
                } finally {
                    _reply.recycle();
                    _data.recycle();
                }
                return _result;
            }

            @Override
            public void addBook(com.joy.ipctest.aidl.Book book) 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 ((book != null)) {
                        _data.writeInt(1);
                        book.writeToParcel(_data, 0);
                    } else {
                        _data.writeInt(0);
                    }
                    mRemote.transact(Stub.TRANSACTION_addBook, _data, _reply, 0);
                    _reply.readException();
                } finally {
                    _reply.recycle();
                    _data.recycle();
                }
            }
        }

    }

    public java.util.List<com.joy.ipctest.aidl.Book> getBookList() throws android.os.RemoteException;

    public void addBook(com.joy.ipctest.aidl.Book book) throws android.os.RemoteException;
}

详细解释可以继续翻看Android开发艺术探索

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值