Android里多进程通讯的方式有许多,广播,内容提供者(ContentProvider),Socket,序列化和Binder等,今天就聊聊序列化和binder吧,也就是我们说的AIDL。
先看个例子吧!看看怎样使用AIDL在不同进程之间传递值。
我们先建三个主要的文件Book.aidl、IBookManager.aidl、Book.java
其中Book.java的内容,就一个实体类,实现了parcelable接口,使用AIDL传递的对象必须要实现该接口
我用的是as3.0.1有自动提示生成下面代码,大家也可以用插件生成
public class Book implements Parcelable {
public int bookId;
public String bookName;
public Book(int bookId, String bookName) {
this.bookId = bookId;
this.bookName = bookName;
}
public 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 parcel, int i) {
parcel.writeInt(bookId);
parcel.writeString(bookName);
}
}
这是Book.aidl的代码,你没有看错,就两行
package com.example.zxw.playexo;
parcelable Book;
这是IBookManager.aidl的代码。定义了两个方法,getBookList和addBook。这里注意,即时book跟他在同一
目录,也要import,这是aidl特殊之处
package com.example.zxw.playexo;
import com.example.zxw.playexo.Book;
interface IBookManager {
List<Book> getBookList();
void addBook(in Book book);
}
上面的是前戏,接下来就开始传值了,这里我说两句,首先用bind启动一个RemoteService服务(这个服务是在另一个进程里面的),那个textview点击事件是往不同进程的service传递Book对象,调用的是addBook方法,也就是我们在上面aidl文件里写的方法,并且调用getBookList方法获取不同进程service 的list,这里为了简单演示就只打印list 的size了
public class MainActivity extends AppCompatActivity {
private IBookManager bookManager;
private int i = 10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bindService(new Intent(this, RemoteService.class),
connection, Context.BIND_AUTO_CREATE);
TextView textView = findViewById(R.id.Hello_text);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
i++;
Book book = new Book(i, "book" + i);
try {
bookManager.addBook(book);
List<Book> bookList = bookManager.getBookList();
Log.e("activity", "onClick: " +bookList.size());
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
}
ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
bookManager = IBookManager.Stub.asInterface(iBinder);
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
};
}
好了,来看看RemoteService的代码,通过IBookManager.Stub的addBook方法获取MainActivity传递过来的book对象,这样我们就完成了从activity到service的传递了。那怎么往回传递呢?其实這里已经验证了,将传递过来的添加到list,然后点击MainAcivity的textview调用getBookList就可以获取到,这里有点绕,大家试试就清楚了
public class RemoteService extends Service {
private Book book;
List<Book> list=new ArrayList<>();
@Nullable
@Override
public IBinder onBind(Intent intent) {
return bookManager;
}
IBookManager.Stub bookManager = new IBookManager.Stub() {
@Override
public List<Book> getBookList() throws RemoteException {
list.add(book);
return list;
}
@Override
public void addBook(Book book) throws RemoteException {
RemoteService.this.book=book;
Log.e("RemoteService", "addBook: " + book.bookName + "------" + book.bookId);
}
};
}
没错就是这么简单 ,究竟AIDL做了什么呢?我们可以看一下IBookManager这个类,你会发现代码有点乱,没关系,ctrl+alt+l,格式化一下代码,并且将stub的代码缩起来先,嘿嘿世界一片光明,这个IBookManager就一个接口,和两个我们写的方法,还有一个stub的静态内部类
好了,打开内部类看看,我擦,还有个内部类Proxy,这不是代理吗?先不看他
我们先是调用了asInterface方法,很简单,判断是否同一进程,是的话玩毛线,不是的话返回这个代理类
再来看看这个代理类,直接实现了最外层的接口IBookManager,重写了我们的两个方法,里面完成了读和写。而外面的onTransact则完成相反的写和读(这里我是猜的)
public static abstract class Stub extends android.os.Binder implements com.example.zxw.playexo.IBookManager {
private static final java.lang.String DESCRIPTOR = "com.example.zxw.playexo.IBookManager";
/**
* Construct the stub at attach it to the interface.
*/
public Stub() {
this.attachInterface(this, DESCRIPTOR);
}
/**
* Cast an IBinder object into an com.example.zxw.playexo.IBookManager interface,
* generating a proxy if needed.
*/
public static com.example.zxw.playexo.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.zxw.playexo.IBookManager))) {
return ((com.example.zxw.playexo.IBookManager) iin);
}
return new com.example.zxw.playexo.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.example.zxw.playexo.Book> _result = this.getBookList();
reply.writeNoException();
reply.writeTypedList(_result);
return true;
}
case TRANSACTION_addBook: {
data.enforceInterface(DESCRIPTOR);
com.example.zxw.playexo.Book _arg0;
if ((0 != data.readInt())) {
_arg0 = com.example.zxw.playexo.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.zxw.playexo.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.example.zxw.playexo.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.zxw.playexo.Book> _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
mRemote.transact(Stub.TRANSACTION_getBookList, _data, _reply, 0);
_reply.readException();
_result = _reply.createTypedArrayList(com.example.zxw.playexo.Book.CREATOR);
} finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
@Override
public void addBook(com.example.zxw.playexo.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);
}
其实AIDL没有什么神秘的,只是帮我们构建了这个类,这个类我们完全可以自己写(随便说说,别当真)AIDL本质只是系统帮我们构建Binder的工具仅此而已!好了,就这样吧!能力有限,有不对的地方望各位大佬指点
感谢参考博客:http://blog.csdn.net/feiduclear_up/article/details/51385868
最后源码传送门:https://github.com/WoBuShuo/PlayExo