选择AIDL分析Binder工作机制
我们新建一个AIDL示例
Book类需要序列化
package com.example.hezhe.myapplication.test;
import android.os.Parcel;
import android.os.Parcelable;
/**
* 创建日期:2018/3/20 on 上午11:57.
* 描述:
* 作者:何喆
*/
public class Book implements Parcelable {
String bookName;
String time;
//内容描述
@Override
public int describeContents() {
return 0;
}
//序列化
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.bookName);
dest.writeString(this.time);
}
public Book() {
}
protected Book(Parcel in) {
this.bookName = in.readString();
this.time = 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后来需要自己改
// IMyAidlInterface.aidl
package com.example.hezhe.myapplication.test;
parcelable Book;
IBookManager.aidl代码需要自己新建然后修改重点要自己import
// IBookManager.aidl
package com.example.hezhe.myapplication.test;
import com.example.hezhe.myapplication.test.Book;
// Declare any non-default types here with import statements
interface IBookManager {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
List<Book> getBookList();
void addBook(in Book book);
}
重新make project生成IBookManager
代码块语法遵循标准markdown代码,例如:
/*
* This file is auto-generated. DO NOT MODIFY.
* Original file: /Users/hezhe/AndroidStudioProjects/MyApplication/app/src/main/aidl/com/example/hezhe/myapplication/test/IBookManager.aidl
*/
package com.example.hezhe.myapplication.test;
// Declare any non-default types here with import statements
public interface IBookManager extends android.os.IInterface
{
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements com.example.hezhe.myapplication.test.IBookManager
{
//binder唯一标识用Binder类名
private static final java.lang.String DESCRIPTOR = "com.example.hezhe.myapplication.test.IBookManager";
/** Construct the stub at attach it to the interface. */
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/**
* Cast an IBinder object into an com.example.hezhe.myapplication.test.IBookManager interface,
* generating a proxy if needed.
*/
//用于服务端的Binder对象转换成客户端所需的AIDL接口类型对象,如何客户端和服务端位于同一进程返回服务端的Stub对象,否则返回系统封装的Stub.proxy对象
public static com.example.hezhe.myapplication.test.IBookManager asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof com.example.hezhe.myapplication.test.IBookManager))) {
return ((com.example.hezhe.myapplication.test.IBookManager)iin);
}
return new com.example.hezhe.myapplication.test.IBookManager.Stub.Proxy(obj);
}
//返回Binder对象
@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.example.hezhe.myapplication.test.Book> _result = this.getBookList();
reply.writeNoException();
reply.writeTypedList(_result);
return true;
}
case TRANSACTION_addBook:
{
data.enforceInterface(DESCRIPTOR);
com.example.hezhe.myapplication.test.Book _arg0;
if ((0!=data.readInt())) {
_arg0 = com.example.hezhe.myapplication.test.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.example.hezhe.myapplication.test.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;
}
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
@Override public java.util.List<com.example.hezhe.myapplication.test.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.example.hezhe.myapplication.test.Book> _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
//rpc 远程过程调用。当前线程挂起,服务端ontransat方法会被调用直到RPC 过程返回
mRemote.transact(Stub.TRANSACTION_getBookList, _data, _reply, 0);
//_data输入型对象,_reply输出型对象
_reply.readException();
_result = _reply.createTypedArrayList(com.example.hezhe.myapplication.test.Book.CREATOR);
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
//没有返回值不需要从_reply中取返回值
@Override public void addBook(com.example.hezhe.myapplication.test.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();
}
}
}
static final int TRANSACTION_getBookList = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
static final int TRANSACTION_addBook = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
}
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
public java.util.List<com.example.hezhe.myapplication.test.Book> getBookList() throws android.os.RemoteException;
public void addBook(com.example.hezhe.myapplication.test.Book book) throws android.os.RemoteException;
}
参考《Android 开发艺术探索》