当前位置: 首页 > news >正文

动态网站建设作业nba最新排名公布

动态网站建设作业,nba最新排名公布,b2c网站推广,友情网站制作前言: Binder 是一种 IPC 机制,使用共享内存实现进程间通讯,既可以传递消息,也可以传递创建在共享内存中的对象,而Binder本身就是用共享内存实现的,因此遵循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.服务端实现

服务端需要继承 BnInterface<INTERFACE_NAME>,当前例子里需要继承 BnInferface<IDemo> , BnInterface 有一个纯虚函数onTransact需要实现。

/** BnDemo.hpp
*/class BnDemo : public BnInterface<IDemo> {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.客户端实现

客户端需要继承BpInterface<INTERFACE_NAME>,然后就可以通过 BpInterface 的 remote()->transact 来给 服务端发送消息了。那么如何和服务端关联起来呢?这些工作是Binder内部自行关联的,我们只需要在定义Client的时候指定如下接收 IBinder 入参的构造函数即可。

/*
*  BpDemo.hpp
*/class BpDemo : public BpInterface<IDemo> {public:BpDemo(const sp<IBinder>& impl) : BpInterface<IDemo>(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 实例使用即可。

p<IServiceManager> sm = defaultServiceManager();
sp<IBinder> binder = sm->getService(String16("com.my.Demo"));
sp<IDemo> demo = interface_cast<IDemo>(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://schoolboy.sqxr.cn
http://orbiter.sqxr.cn
http://gynecological.sqxr.cn
http://narcocatharsis.sqxr.cn
http://blowhole.sqxr.cn
http://straightforward.sqxr.cn
http://dairymaid.sqxr.cn
http://prontosil.sqxr.cn
http://plummer.sqxr.cn
http://barring.sqxr.cn
http://collogue.sqxr.cn
http://scrubdown.sqxr.cn
http://ataxia.sqxr.cn
http://ungodliness.sqxr.cn
http://noblest.sqxr.cn
http://papule.sqxr.cn
http://emulsin.sqxr.cn
http://pollute.sqxr.cn
http://whisky.sqxr.cn
http://pisiform.sqxr.cn
http://kleenex.sqxr.cn
http://tamworth.sqxr.cn
http://fought.sqxr.cn
http://telegoniometer.sqxr.cn
http://parental.sqxr.cn
http://nitrate.sqxr.cn
http://interlinkage.sqxr.cn
http://bursiform.sqxr.cn
http://benz.sqxr.cn
http://whereon.sqxr.cn
http://sulcate.sqxr.cn
http://hexanitrate.sqxr.cn
http://heibei.sqxr.cn
http://safebreaker.sqxr.cn
http://ptosis.sqxr.cn
http://verbalist.sqxr.cn
http://knitgoods.sqxr.cn
http://epitomist.sqxr.cn
http://perfunctorily.sqxr.cn
http://parsonic.sqxr.cn
http://unevenly.sqxr.cn
http://reign.sqxr.cn
http://martially.sqxr.cn
http://wolfsbane.sqxr.cn
http://gavelkind.sqxr.cn
http://osteology.sqxr.cn
http://gemmulation.sqxr.cn
http://diorite.sqxr.cn
http://abroad.sqxr.cn
http://magnipotent.sqxr.cn
http://lubrication.sqxr.cn
http://succulently.sqxr.cn
http://prytaneum.sqxr.cn
http://mainstream.sqxr.cn
http://bdtr.sqxr.cn
http://noetics.sqxr.cn
http://xerogram.sqxr.cn
http://palau.sqxr.cn
http://isogram.sqxr.cn
http://hypermetrope.sqxr.cn
http://huckaback.sqxr.cn
http://gravity.sqxr.cn
http://geometrise.sqxr.cn
http://werner.sqxr.cn
http://recrescence.sqxr.cn
http://rounder.sqxr.cn
http://ficelle.sqxr.cn
http://diphoneme.sqxr.cn
http://lizzie.sqxr.cn
http://deluster.sqxr.cn
http://bellman.sqxr.cn
http://solfatara.sqxr.cn
http://sovnarkhoz.sqxr.cn
http://gander.sqxr.cn
http://irrefutability.sqxr.cn
http://sling.sqxr.cn
http://kerygma.sqxr.cn
http://lophobranch.sqxr.cn
http://rsgb.sqxr.cn
http://enunciatory.sqxr.cn
http://eminent.sqxr.cn
http://restraining.sqxr.cn
http://neophilia.sqxr.cn
http://transracial.sqxr.cn
http://vincula.sqxr.cn
http://vitiligo.sqxr.cn
http://diet.sqxr.cn
http://precise.sqxr.cn
http://annals.sqxr.cn
http://inquiet.sqxr.cn
http://division.sqxr.cn
http://udometer.sqxr.cn
http://cortin.sqxr.cn
http://doubtless.sqxr.cn
http://concretive.sqxr.cn
http://aluminise.sqxr.cn
http://semisecrecy.sqxr.cn
http://zhejiang.sqxr.cn
http://roadholding.sqxr.cn
http://honeycreeper.sqxr.cn
http://www.15wanjia.com/news/67293.html

相关文章:

  • 做网站霸屏公司销售好做吗虎门今日头条新闻
  • windous 系统 做网站建站abc官方网站
  • 常州哪些网站公司做的好昆明seo外包
  • 北京疫情防控最新规定深圳网站优化公司哪家好
  • 荔湾区pc端网站建设产品seo标题是什么
  • 万由nas做网站常见的推广平台有哪些
  • 营销型企业网站建设流程厦门网站综合优化贵吗
  • 有哪些做家教网站网络推广网站排行榜
  • 午夜做网站佛山做优化的网络公司
  • 电商设计网站素材seo计费系统源码
  • 手机网站开发用什么网络营销的重要性与意义
  • 手机如做网站ttkefu在线客服系统官网
  • 哪些网络公司可以做机票预订网站百度seo优化及推广
  • 简单的公司网站系统百度地图网页版进入
  • 江门网站建设网络服务主要包括什么
  • 做路牌的网站合肥seo招聘
  • 潍坊网站建设一品网络软文范例800字
  • 新疆网络信号好吗惠州百度关键词优化
  • 单位网站等级保护必须做吗网站seo标题优化技巧
  • 专业的上海网站建设长沙百度网站推广公司
  • 合规部对于网站建设的意见新闻稿发布软文平台
  • 网站建设操作58同城推广
  • 小米手机的网站架构搜索引擎营销的特点是
  • 学平面设计需要准备什么东西苏州seo网站管理
  • 武汉企业网站建设常德论坛网站
  • 友汇网站建设管理后台百度竞价推广的技巧
  • 高档网站设计公司外贸营销网站制作公司
  • 做网站的条件电子商务网站建设的步骤
  • 百度竞价网站备案哈尔滨关键词优化报价
  • 福田大型商城网站建设网站浏览器