网站建设与管理习题一外贸网站经典营销案例
网站建设与管理习题一,外贸网站经典营销案例,公司变更告知函怎么写,松江网站建设前言#xff1a;
Binder 是一种 IPC 机制#xff0c;使用共享内存实现进程间通讯#xff0c;既可以传递消息#xff0c;也可以传递创建在共享内存中的对象#xff0c;而Binder本身就是用共享内存实现的#xff0c;因此遵循Binder写法的类是可以实例化后在进程间传递的。…前言
Binder 是一种 IPC 机制使用共享内存实现进程间通讯既可以传递消息也可以传递创建在共享内存中的对象而Binder本身就是用共享内存实现的因此遵循Binder写法的类是可以实例化后在进程间传递的。
Binder在Android架构中有很重的地位各个模块都在重度使用它从代码可读性角度看在为熟悉之前的可读性较差。从整体架构的角度看在各个模块中通过IPC分离接口和实现有效地提高了扩展性。 写法
1.定义接口
这个接口需要 Client/Bp 和 Server/Bn 都要遵循Client需要关注有哪些消息类型即enum值Server需要关注虚函数主要任务是根据消息中的enum值判断不同的业务类型然后调用相应的虚函数。
/** IDemo.h
*/class IDemo : public IInterface {public:enum {ALERT IBinder::FIRST_CALL_TRANSACTION,PUSH,ADD};// Sends a user-provided value to the servicevirtual void push(int32_t data) 0;// Sends a fixed alert string to the servicevirtual void alert() 0;// Requests the service to perform an addition and return the resultvirtual int32_t add(int32_t v1, int32_t v2) 0;DECLARE_META_INTERFACE(Demo);
};// This implementation macro would normally go in a cpp file
IMPLEMENT_META_INTERFACE(Demo, com.my.Demo);
2.服务端实现
服务端需要继承 BnInterfaceINTERFACE_NAME当前例子里需要继承 BnInferfaceIDemo , BnInterface 有一个纯虚函数onTransact需要实现。
/** BnDemo.hpp
*/class BnDemo : public BnInterfaceIDemo {virtual status_t onTransact(uint32_t code, const Parcel data,Parcel* reply, uint32_t flags 0);
};status_t BnDemo::onTransact(uint32_t code, const Parcel data,Parcel* reply, uint32_t flags) {data.checkInterface(this);switch(code) {case ALERT: {alert();return NO_ERROR;} break;case PUSH: {int32_t inData data.readInt32();push(inData);return NO_ERROR;} break;case ADD: {int32_t inV1 data.readInt32();int32_t inV2 data.readInt32();int32_t sum add(inV1, inV2);reply-writeInt32(sum);return NO_ERROR;} break;default:return BBinder::onTransact(code, data, reply, flags);}
}
如果不希望把业务代码和Binder架构代码混到一起那么就再定义一个类来实现业务层。
/** Demo.hpp
*/class Demo : public BnDemo {virtual void push(int32_t data) {// Do something with the data the client pushed}virtual void alert() {// Handle the alert}virtual int32_t add(int32_t v1, int32_t v2) {return v1 v2;}
};
3.客户端实现
客户端需要继承BpInterfaceINTERFACE_NAME然后就可以通过 BpInterface 的 remote()-transact 来给 服务端发送消息了。那么如何和服务端关联起来呢这些工作是Binder内部自行关联的我们只需要在定义Client的时候指定如下接收 IBinder 入参的构造函数即可。
/*
* BpDemo.hpp
*/class BpDemo : public BpInterfaceIDemo {public:BpDemo(const spIBinder impl) : BpInterfaceIDemo(impl) { }virtual void push(int32_t push_data) {Parcel data, reply;data.writeInterfaceToken(IDemo::getInterfaceDescriptor());data.writeInt32(push_data);remote()-transact(PUSH, data, reply);}virtual void alert() {Parcel data, reply;data.writeInterfaceToken(IDemo::getInterfaceDescriptor());remote()-transact(ALERT, data, reply, IBinder::FLAG_ONEWAY);}virtual int32_t add(int32_t v1, int32_t v2) {Parcel data, reply;data.writeInterfaceToken(IDemo::getInterfaceDescriptor());data.writeInt32(v1);data.writeInt32(v2);remote()-transact(ADD, data, reply);int32_t res;status_t status reply.readInt32(res);return res;}
};
4.如何使用
服务端注册服务给系统的服务管理器。
defaultServiceManager()-addService(String16(com.my.Demo), new Demo());
客户端在系统的服务管理器中根据 标识字符串 查找相应的服务把返回的值cast 为 INTERFACE 实例使用即可。
pIServiceManager sm defaultServiceManager();
spIBinder binder sm-getService(String16(com.my.Demo));
spIDemo demo interface_castIDemo(binder);demo-alert();
demo-push(65);
int32_t sum demo-add(453, 827);
5.代码阅读
服务端如何查找service的标识字符串 代码中搜索 addService 客户端如何查找连接了哪个Binder服务端 代码中搜索 getService 记住上面两点能够快速定位客户端和服务端的关联关系。 参考
https://android.googlesource.com/platform/frameworks/native//jb-mr1-dev/include/binder/IInterface.h
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/88631.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!