1.什么是NativeService Native Service,是通过C或C++代码写出來,提供给Java进行远程调用的RemoteService。向Android开机就启动的surfaceflinger,media都是native service。
2.BnXXX 和 BpXXX 如果需要通过Native代码來提供服务:
3. 怎樣寫Native Service? 和Java环境里的aidl工具类似。Native也会使用重用技术。它的重用是通过template 体现的。 一些模板类都在IInterface中。 /frameworks/native/include/binder/IInterface.h 1 class IInterface : public virtual RefBase 2 { 3 public: 4 IInterface(); 5 sp<IBinder> asBinder(); 6 sp<const IBinder> asBinder() const; 7 8 protected: 9 virtual ~IInterface(); 10 virtual IBinder* onAsBinder() = 0; 11 }; 12 13 template<typename INTERFACE> 14 class BnInterface : public INTERFACE, public BBinder//实现Stub功能的模板,扩展BBinder的onTransact()方法实现Binder命令的解析和执行。 15 { 16 public: 17 virtual sp<IInterface> queryLocalInterface(const String16& _descriptor); 18 virtual const String16& getInterfaceDescriptor() const; 19 20 protected: 21 virtual IBinder* onAsBinder(); 22 }; 23 24 template<typename INTERFACE> 25 class BpInterface : public INTERFACE, public BpRefBase//实现Proxy功能的模板,BpRefBase里有个mRemote对象指向一个BpBinder对象。 26 { 27 public: 28 BpInterface(const sp<IBinder>& remote); 29 30 protected: 31 virtual IBinder* onAsBinder(); 32 };
来看一下类结构:
有了template,编写一个Native Service的工作量也不大。比如如果将第一篇中的Application RemoteService 转化成Native Service:
如果实现了native的RemoteService,会是下面的结构。红框框就是我们要写的。
Native Service只要稍微了解一下,看得懂代码流程就好了,毕竟应该没什么机会去写native service吧。
|
|
来自: just_person > 《Binder项目》