Understand the principle of Binder mechanism:
It is an inter process communication mode, CS architecture, user space and kernel space
C++:
The message is handled by a subclass of BnInterface
Send message:
status_t transact(uint32_t code,const Parcel& data,Parcel* reply,uint32_t flags = 0);
Process message:
status_t onTransact(uint32_t code, const Parcel& data,Parcel* reply, uint32_t flags)
Basics:
The p in BpInterface and BpBinder means proxy, which means intermediate calls and proxies and will not specifically implement functions
The n in BnInterface and BBinder means that native is the final implementation
The BpBinder class is inherited by the client. Of course, if it is not available, it can also be omitted
If a class wants to use the smart pointer in android, it must inherit or indirectly inherit the RefBase class, such as client
Inheritance relationship
Define an interface class IDemo, which inherits IInterface
Define a BpDemoService class that inherits bpinterface < idemo >
Define a BnDemoService class and inherit bninterface < idemo >
Define a DemoService class that inherits BnDemoService and is used to implement specific functions
Use case:
First define an interface class IDemo, then define a proxy class BpDemoService, a native interface BnDemoService, and then define an implementation class DemoService that inherits BnDemoService to implement specific functions. Finally, create a client side. Call sequence: first obtain the object of servicemanager, then create a binder to load the service and call the interface_cast() gets the object of the service proxy end, and finally calls the interface.
Define an interface class IDemo,inherit IInterface,There is only one function: class IDemo :public IInterface { public: virtual int getAge(); } Define a BpDemoService Class, inheritance BpInterface enum { CUSTOM_CODE = IBinder::FIRST_CALL_TRANSACTION }; class BpDemoService: public BnInterface<IDemo> { public: BpDemoService(const sp<IBinder>& impl):BpInterface<IDemo> (impl){}; virtual BOOL getAge() { Parcel data, reply; data.writeInterfaceToken(IDemo::getInterfaceDescriptor()); remote()->transact(CUSTOM_CODE, data, &reply);//send message return (BOOL)reply.readInt32(); } } Define a BnDemoService Class, inheritance BnInterface class BnDemoService: public BnInterface<IDemo> { public: virtual status_t onTransact(uint32_t code, const Parcel& data,Parcel* reply, uint32_t flags= 0 ); }; //Accept message processing status_t BnDemoService::onTransact(uint32_t code, const Parcel& data,Parcel* reply, uint32_t flags) { switch(code) { case CUSTOM_CODE: { CHECK_INTERFACE(IDemo,data,reply); int res = getAge(); reply->writeInt32((int32_t)res); return NO_ERROR; } break; default: break; } return BBinder::onTransact(code, data, reply, flags); } Define a Service Class, inheritance BnDemoService Used to implement functions. Finally, remember to call IMPLEMENT_META_INTERFACE Macro to register class DemoService : public BnDemoService,public BinderService<DemoService> { public: int getAge(); } IMPLEMENT_META_INTERFACE(DemoService, "demo.service");
Call order
- When calling on the client side, first get the ServiceManager
- Then create a binder to load the service
- Using interface_cast() to get the corresponding proxy
- Call the functions implemented in the service through the proxy
sp < IServiceManager > sm = defaultServiceManager(); sp < IBinder > binder; binder = sm->getService(String16("demo.service")); sp<IDemo> m_pDemoService; m_pDemoService = interface_cast<IDemo> (binder); m_pDemoService->getAge();
interface_cast() function
The function is to forcibly convert the Binder obtained from getService into a subclass of iiinterface, that is, the classes that define their own interfaces
Remote() - > transact() function
The location is invoked in the proxy class, which is used to send messages and data to onTransact functions remotely, similar to handler.sendMessage().
onTransact() function
The position of this function is in the implementation class (implementing BnInterface), which is equivalent to the handleMessage function in the handler
JAVA:
quote: