1.什么是NativeService
Native Service,是通過C或C++代碼寫出來,提供給Java進行遠程調用的RemoteService。向Android開機就啟動的surfaceflinger,media都是native service。
在前一篇中,我們總結了Binder通信的整個流程:
Java Proxy代碼走到JNI實現的BinderProxy的transact()方法之后,就直接進入到Native實現的BpBinder,然后一直通過IPCThreadState對象發送Binder消息。
而在另一個process的IPCThreadState會接收Binder消息,再通過JNI回調Java里的Stub的onTransact()方法。
2.BnXXX 和 BpXXX
如果需要通過Native代碼來提供服務:
從IBinder接口的Stub對象的原理可以看出,如果在回調Java的JNI之前將代碼調用截斷,直接通過Native代碼來實現onTransact()方法,就可以完成Service的Stub端的實現。
同時,RemoteService應該不僅提供給Java,也可以提供給Native,所以也應該提供Native的Proxy端,就是直接通過BpBinder的transact()方法來發送Binder消息。
下面圖中的 BnXXX和BpXXX對應着Java環境里的的Stub和Proxy
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:
1.實現一個接口文件,IXXXService,繼承IInterface
2.定義BnXXX,繼承BnInterface<IXXXService>。實現一個XXXService,繼承BnXXX,並實現onTransact()函數。
3.定義BpXXX,繼承BpInterface<IXXXService>。
如果實現了native的RemoteService,會是下面的結構。紅框框就是我們要寫的。
Native Service只要稍微了解一下,看得懂代碼流程就好了,畢竟應該沒什么機會去寫native service吧。