一、AIDL
1. Book.java 实现Parcelable接口
package com.android.shieh.processtest.aidl; import android.os.Parcel; import android.os.Parcelable; /** * Created by GKX100187 on 2015/11/27. */ public class Book implements Parcelable { public int bookId; public String bookName; public Book(int bookId, String bookName){ this.bookId = bookId; this.bookName = bookName; } private Book(Parcel in) { bookId = in.readInt(); bookName = in.readString(); } public static final Creator<Book> CREATOR = new Creator<Book>() { @Override public Book createFromParcel(Parcel in) { return new Book(in); } @Override public Book[] newArray(int size) { return new Book[size]; } }; @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeInt(bookId); dest.writeString(bookName); } }
2. Book.aidl 将Book类在AIDL中声明
package com.android.shieh.processtest.aidl; parcelable Book;//声明Book类
3.BookManager.aidl 定义接口
// IBookManager.aidl package com.android.shieh.processtest.aidl; // Declare any non-default types here with import statements import com.android.shieh.processtest.aidl.Book;//在同一个包中也要import该类 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); }
IDE会根据该接口自动生成相应的.java类,类似手动创建中的BookManagerImpl类
二、手动创建
1.AIDL性质的接口声明IBookManager.java
package com.android.shieh.processtest.manualbinder; import android.os.IBinder; import android.os.IInterface; import android.os.RemoteException; import com.android.shieh.processtest.aidl.Book; import java.util.List; /** * Created by GKX100187 on 2015/11/30. */ public interface IBookManager extends IInterface { static final String DESCRIPTOR //Binder的唯一标识符,一般用当前Binder类名表示 = "com.android.shieh.processtest.manualbinder.IBookManager"; static final int TRANSACTION_getBookList = IBinder//对应方法的id .FIRST_CALL_TRANSACTION + 0; static final int TRANSACTION_addBook = IBinder .FIRST_CALL_TRANSACTION + 1; public List<Book> getBookList() throws RemoteException; public void addBook(Book book) throws RemoteException; }
2.实现Stub类和Stub类中的代理类
package com.android.shieh.processtest.manualbinder; import android.os.Binder; import android.os.IBinder; import android.os.IInterface; import android.os.Parcel; import android.os.RemoteException; import com.android.shieh.processtest.aidl.Book; import java.util.List; /** * Created by GKX100187 on 2015/11/30. */ public class BookManagerImpl extends Binder implements IBookManager{ public BookManagerImpl() { //接口黏合 this.attachInterface(this,DESCRIPTOR); } /**用于将服务端的Binder对象转换成客户端所需要AIDL接口类型的对象 *客户端与服务端处于同一进程,返回的是服务端Stub本身 * 否则返回的是系统封装后的Stub.proxy(代理Stub,供客户端使用) */ public static IBookManager asInterface(IBinder obj){ if(obj == null){ return null; } IInterface iin = obj.queryLocalInterface(DESCRIPTOR); if(((iin != null) && (iin instanceof IBookManager))){ return ((IBookManager)iin); } return new BookManagerImpl.Proxy(obj); } /** * 返回当前的Binder对象 * */ @Override public IBinder asBinder() { return this; } /** * 该方法运行在服务端Binder线程池, * 服务端通过code确定客户端所请求的方法, * data中取出所需参数 * reply写入返回值 * 返回false表示可能客户端请求失败 * */ @Override protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException { switch (code){ case INTERFACE_TRANSACTION: reply.writeString(DESCRIPTOR); return true; case TRANSACTION_getBookList: data.enforceInterface(DESCRIPTOR); List<Book> result = this.getBookList(); reply.writeNoException(); reply.writeTypedList(result); return true; case TRANSACTION_addBook: data.enforceInterface(DESCRIPTOR); Book arg0; if(data.readInt()!=0){ arg0 = Book.CREATOR.createFromParcel(data); }else { arg0 = null; } reply.writeNoException(); return true; } return super.onTransact(code, data, reply, flags); } @Override public List<Book> getBookList() throws RemoteException { return null; } @Override public void addBook(Book book) throws RemoteException { } private static class Proxy implements IBookManager{ private IBinder mRemote; Proxy(IBinder remote){ mRemote = remote; } @Override public IBinder asBinder() { return mRemote; } public String getInterfaceDescriptor() { return DESCRIPTOR; } /** * 运行在客户端 * */ @Override public List<Book> getBookList() throws RemoteException { Parcel data = Parcel.obtain(); Parcel reply = Parcel.obtain(); List<Book> result; try { data.writeInterfaceToken(DESCRIPTOR); //发送远程调用请求,阻塞等待,调用服务端的onTransact()返回后继续执行 mRemote.transact(TRANSACTION_getBookList,data,reply,0); //此时服务端已经处理完客户端的请求 reply.readException();//客户端取出返回值 result = reply.createTypedArrayList(Book.CREATOR); }finally { reply.recycle(); data.recycle(); } return result; } @Override public void addBook(Book book) throws RemoteException { Parcel data = Parcel.obtain(); Parcel reply = Parcel.obtain(); try{ data.writeInterfaceToken(DESCRIPTOR); if((book != null)) { data.writeInt(1); book.writeToParcel(data, 0); } else { data.writeInt(0); } mRemote.transact(TRANSACTION_addBook,data,reply,0); reply.readException(); }finally { reply.recycle(); data.recycle(); } } } }
三、Binder的死亡代理 DethRecipient
private IBinder.DeathRecipient mDeathRecipient = new IBinder.DeathRecipient(){ @Override public void binderDied() { if(mBookManager == null){ return ; } mBookManager.asBinder().unlinkToDeath(mDeathRecipient,0); mBookManager = null; //再重新绑定远程Service } };
客户端绑定远程服务,给Binder设置死亡代理
mService = IMessageBoxManager.Stub.asInterface(binder); binder.linkToDeath(mDeathRecipient);