IPC机制(二)

来源:互联网 发布:matlab字符串存入数组 编辑:程序博客网 时间:2024/05/02 01:09
一、ContentProvider
用于不同应用间数据共享,底层实现还是Binder
主要抽象方法:
(1)onCreate,由系统调用,运行在主线程。
(2)query、update、insert、delete,对应CRUD,由外界调用,运行在Binder线程池。
(3)getType:返回一个Uri对应的MIME类型(媒体类型)
二、Socket
套接字。分为流式套接字(TCP)和用户数据报套接字(UDP)两种。
三、Binder连接池
原理:如果需要多个AIDL接口的时候,只需要一个Service,服务端提供一个queryBinder接口,根据业务模块返回响应的Binder对象给他们进行远程调用。Binder连接池作用是将所有的Binder请求统一在远程Service中去执行。
1、创建AIDL接口
//Binder连接池interface IBinderPool {    IBinder queryBinder(int binderCode);}
2、BinderPool 连接池
public class BinderPool {    public static final int BOOK_BINDERCODE = 1000;    private static volatile BinderPool sInstance;    private Context mContext;    private IBinderPool mBinderPool;    private CountDownLatch mCountDownLatch;    private BinderPool(Context context){        mContext = context.getApplicationContext();        connectBinderPoolService();    }    public static BinderPool getInstance(Context context){        if (sInstance==null){            synchronized (BinderPool.class){                if (sInstance==null){                    sInstance = new BinderPool(context);                }            }        }        return sInstance;    }    private IBinder.DeathRecipient mRecipient = new IBinder.DeathRecipient(){        @Override        public void binderDied() {            mBinderPool.asBinder().unlinkToDeath(mRecipient,0);            mBinderPool = null;            //死了重连            connectBinderPoolService();        }    };    //绑定服务    private void connectBinderPoolService() {        mCountDownLatch = new CountDownLatch(1);        Intent service = new Intent(mContext,BinderPoolService.class);        mContext.bindService(service,connection,Context.BIND_AUTO_CREATE);        try {            mCountDownLatch.await();        }catch (InterruptedException e){            e.printStackTrace();        }    }    private ServiceConnection connection = new ServiceConnection() {        @Override        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {            mBinderPool = IBinderPool.Stub.asInterface(iBinder);            try {                mBinderPool.asBinder().linkToDeath(mRecipient,0);            }catch (RemoteException e){                e.printStackTrace();            }            mCountDownLatch.countDown();        }        @Override        public void onServiceDisconnected(ComponentName componentName) {        }    };    //根据不同的binderCode返回不同的binder    public IBinder queryBinder(int binderCode){        IBinder binder = null;        try{            if (mBinderPool!=null){                binder = mBinderPool.queryBinder(binderCode);            }        }catch (RemoteException e){            e.printStackTrace();        }        return binder;    }    public static class BinderPoolImpl extends IBinderPool.Stub{        @Override        public IBinder queryBinder(int binderCode) throws RemoteException {            IBinder binder = null;            switch (binderCode){                case BOOK_BINDERCODE:                    binder = new BookManagerImpl();                    break;            }            return binder;        }    }}
3、AIDL实现类
public class BookManagerImpl extends IBookManager.Stub {    @Override    public List<Book> getBookList() throws RemoteException {        return null;    }    @Override    public void add(Book book) throws RemoteException {    }    @Override    public void registListener(IOnNewBookArrivedListener listener) throws RemoteException {    }    @Override    public void unregistListener(IOnNewBookArrivedListener listener) throws RemoteException {    }}
4、服务端Service
public class BinderPoolService extends Service {    private Binder mBinder = new BinderPool.BinderPoolImpl();    @Nullable    @Override    public IBinder onBind(Intent intent) {        return mBinder;    }}
5、用户端的调用
private void doWork() {    BinderPool binderPool = BinderPool.getInstance(MainActivity.this);    IBinder binder = binderPool.queryBinder(BinderPool.BOOK_BINDERCODE);    //获取图片管理的binder    mBookManager = BookManagerImpl.asInterface(binder);    //调用方法    try {        mBooks = mBookManager.getBookList();    }catch (RemoteException e){        e.printStackTrace();    }}
四、合适的IPC方式:








0 0