Android Binder机制入门

Binder机制是什么?

仅从应用层上来讲:

  • Binder是一个类,实现了IBinder接口
  • Binder是android中的一种跨进程通信方式,Binder基于C/S模型,是客户端和服务端通信的一种媒介,当执行bindService的时候,服务端会返回一个IBinder对象,客户端可以根据这个IBinder对象获取相应的服务端的服务。

为什么要使用Binder

android中的IPC机制虽然有很多中,比如文件共享,Socket,Messager,ContentProvider,四大组件间通过Bundle传递数据等(Messager,ContentProvider都是通过封装Binder实现的),但是只有Binder机制能很好的实现RPC(远程服务调用),并且在高并发的情况下,Binder能更好的处理好线程同步问题。

Binder的使用Demo

实现Binder有两中方式:

  • 定义好对应的AIDL接口,使用android studio等IDE自动生成Binder的实现类。
  • 自己手写Binder实现类。

通过AIDL生成Binder

首先,aidl支持的数据类型有:

  • 所有的基本类型
  • String
  • CharSequence
  • List
  • Map
  • 实现了Parcelable接口的自定义类型
  1. 所以如果我们需要传输自定义类型,则需要实现Parcelable接口:
public class Book implements Parcelable {
  1. 编写aidl接口:
import com.jason.binderdemo.Book;
interface IBookManager {
    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
    List getBookList();

    void addBook(in Book book);
}

注意,如果使用自定义的类型,需要手动import包名,并且需要在生成一个同名的aidl文件:

// Book.aidl.aidl
package com.jason.binderdemo;

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

parcelable Book;
  1. 编译后,IDE会为我们自动生成一个与aidl接口同名的interface:

可以在build/generated/source/aidl文件夹下找到

public interface IBookManager extends android.os.IInterface
{
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements com.jason.binderdemo.IBookManager
{
private static final java.lang.String DESCRIPTOR = "com.jason.binderdemo.IBookManager";
/** Construct the stub at attach it to the interface. */
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/**
 * Cast an IBinder object into an com.jason.binderdemo.IBookManager interface,
 * generating a proxy if needed.
 */
public static com.jason.binderdemo.IBookManager asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof com.jason.binderdemo.IBookManager))) {
return ((com.jason.binderdemo.IBookManager)iin);
}
return new com.jason.binderdemo.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 _result = this.getBookList();
reply.writeNoException();
reply.writeTypedList(_result);
return true;
}
case TRANSACTION_addBook:
{
data.enforceInterface(DESCRIPTOR);
com.jason.binderdemo.Book _arg0;
if ((0!=data.readInt())) {
_arg0 = com.jason.binderdemo.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.jason.binderdemo.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 getBookList() throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
java.util.List _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
mRemote.transact(Stub.TRANSACTION_getBookList, _data, _reply, 0);
_reply.readException();
_result = _reply.createTypedArrayList(com.jason.binderdemo.Book.CREATOR);
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
@Override public void addBook(com.jason.binderdemo.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 getBookList() throws android.os.RemoteException;
public void addBook(com.jason.binderdemo.Book book) throws android.os.RemoteException;
}

这个类主要由一个接口和他的实现类Stub组成,而Stub又继承自Binder类,也就是为我们自动生成了一个包含aidl中声明方法的Binder类。
其实第二种生成的Binder的方法也就是我们自己去编写这个类

  1. 在服务端的onBind方法中返回binder对象:
    IBookManager.Stub mBinder = new IBookManager.Stub() {
        @Override
        public List getBookList() throws RemoteException {
            return mList;
        }

        @Override
        public void addBook(Book book) throws RemoteException {
            mList.add(book);
        }
    };
    ...
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }
  1. 在客户端调用bindService通过ServiceConnection拿到服务端的接口服务:
    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
         //   mRemote = IBookManager.Stub.asInterface(service);
            mRemote = IBBookManagerImpl.asInterface(service);
            isBind = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mRemote = null;
            isBind = false;
        }
    };
    ...
    @Override
    protected void onStart() {
        super.onStart();
        if (!isBind) {
            Intent intent = new Intent(this, RemoteService.class);
            bindService(intent, connection, BIND_AUTO_CREATE);
        }
    }
  1. 设置Binder死亡的回调:
    private IBinder.DeathRecipient mDeathRecipient = new IBinder.DeathRecipient() {
        @Override
        public void binderDied() {
            if (mRemote != null) {
                mRemote.asBinder().unlinkToDeath(mDeathRecipient, 0);
                mRemote = null;
                //重新绑定服务
            }
        }
    };
    ...
         public void onServiceConnected(ComponentName name, IBinder service) {
         //   mRemote = IBookManager.Stub.asInterface(service);
            mRemote = IBBookManagerImpl.asInterface(service);
            try {
                service.linkToDeath(mDeathRecipient, 0);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
            isBind = true;
        }
    ...
  1. 通过调用服务端暴露的接口调用对应的操作:
    public void addBook(View v) {
        Book book = new Book(3, "got3");
        try {
            mRemote.addBook(book);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public void getBook(View v){
        try {
            Book book = mRemote.getBookList().get(0);
            mTvInfo.setText(book.bookId + "\n" + book.bookName);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

手动编写Binder的实现类

  1. 参照系统生成的IBookManger接口,手写一个类似的接口:
public interface IBBookManager extends IInterface{

    static final String DESCRIPTOR = "com.jason.binderdemo.IBookManager"; //Binder的标记

    static final int TRANSACTION_getBookList = IBinder.FIRST_CALL_TRANSACTION + 0; //标记调用哪个方法
    static final int TRANSACTION_addBook = IBinder.FIRST_CALL_TRANSACTION + 1;

    List getBookList() throws RemoteException; //aidl文件中声明的接口
    void addBook(Book book) throws RemoteException;

}

这个接口包含了服务端提供的服务以及对应方法的标记字段和用于区别其他Binder的DESCRIPTOR字段

  1. 编写一个继承自Binder并且实现IBBookManager接口的抽象类:
public abstract class IBBookManagerImpl extends Binder implements IBBookManager {

    public IBBookManagerImpl() {
        //系统的构造方法
        this.attachInterface(this, DESCRIPTOR);
    }

    @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 result = this.getBookList(); //接收到客户端的请求后调用本地的方法
                reply.writeNoException();
                reply.writeTypedList(result); //在相应中写入数据
                return true;
            case TRANSACTION_addBook:
                data.enforceInterface(DESCRIPTOR);
                Book arg0;
                if (0!=data.readInt()) {
                    arg0 = Book.CREATOR.createFromParcel(data); //将参数反序列化为BOOK对象
                }else {
                    arg0 = null;
                }
                this.addBook(arg0); //调用本地方法
                reply.writeNoException();
                return true;
        }
        return super.onTransact(code, data, reply, flags);
    }

    public static IBBookManager asInterface(IBinder obj) { //为客户端暴露服务端提供的服务
        if (obj == null) {
            return null;
        }
        IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
        if (iin != null && iin instanceof IBBookManager) {
            //跑在统一进程中,其实就是返回当前这个对象,即在构造函数中传入的this
            //调用方法时不需要通过transact过程
            return ((IBBookManager)iin);
        }else {
            //不同进程通过代理类来完成跨进程的调用
            return new Proxy(obj);
        }
    }
  1. 编写代理类
    private static class Proxy implements IBBookManager {

        private IBinder mRemote;

        public Proxy(IBinder remote) {
            this.mRemote = remote;
        }

        public String getInterfaceDescriptor() {
            return DESCRIPTOR;
        }

        @Override
        public List getBookList() throws RemoteException {
            Parcel data = Parcel.obtain();
            Parcel reply = Parcel.obtain();
            List result;
            try {
                data.writeInterfaceToken(DESCRIPTOR); //写入Binder信息用于验证
                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);//写入Binder信息用于验证
                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();
            }
        }

        @Override
        public IBinder asBinder() {
            return mRemote;
        }
    }

接下里就可以用编写的这几个类进行跨进程通信了。aidl只是系统提供了一个快速实现Binder的方法。

Binder机制的调用解析

Android Binder机制入门_第1张图片
Binder工作流程

以上为服务端和客户端跑在不同进程的调用过程,如果服务端和客户端是在同一进程就可以直接调用服务端的方法获取结果。

你可能感兴趣的:(Android Binder机制入门)