首页 技术 正文
技术 2022年11月16日
0 收藏 942 点赞 3,599 浏览 1797 个字

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()方法。

Binder机制,从Java到C (7.  Native Service)

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

Binder机制,从Java到C (7.  Native Service)

3. 怎樣寫Native Service?
如果要手动实现各个类,会造成代码的大量重复,并且出错的几率会大大增加。

和Java环境里的aidl工具类似。Native也会使用重用技术。它的重用是通过template 体现的。

一些模板类都在IInterface中。

/frameworks/native/include/binder/IInterface.h

 class IInterface : public virtual RefBase
{
public:
IInterface();
sp<IBinder> asBinder();
sp<const IBinder> asBinder() const; protected:
virtual ~IInterface();
virtual IBinder* onAsBinder() = ;
}; template<typename INTERFACE>
class BnInterface : public INTERFACE, public BBinder//实现Stub功能的模板,扩展BBinder的onTransact()方法实现Binder命令的解析和执行。
{
public:
virtual sp<IInterface> queryLocalInterface(const String16& _descriptor);
virtual const String16& getInterfaceDescriptor() const; protected:
virtual IBinder* onAsBinder();
}; template<typename INTERFACE>
class BpInterface : public INTERFACE, public BpRefBase//实现Proxy功能的模板,BpRefBase里有个mRemote对象指向一个BpBinder对象。
{
public:
BpInterface(const sp<IBinder>& remote); protected:
virtual IBinder* onAsBinder();
};

来看一下类结构:

Binder机制,从Java到C (7.  Native Service)

有了template,编写一个Native Service的工作量也不大。比如如果将第一篇中的Application RemoteService 转化成Native Service:
1.实现一个接口文件,IXXXService,继承IInterface
2.定义BnXXX,继承BnInterface<IXXXService>。实现一个XXXService,继承BnXXX,并实现onTransact()函数。
3.定义BpXXX,继承BpInterface<IXXXService>。

如果实现了native的RemoteService,会是下面的结构。红框框就是我们要写的。

Binder机制,从Java到C (7.  Native Service)

Native Service只要稍微了解一下,看得懂代码流程就好了,毕竟应该没什么机会去写native service吧。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,291