于是函数继续往下执行:
- jobject object = (jobject)val->findObject(&gBinderProxyOffsets);
于是函数又继续往下执行:
- object = env->NewObject(gBinderProxyOffsets.mClass, gBinderProxyOffsets.mConstructor);
- env->SetIntField(object, gBinderProxyOffsets.mObject, (int)val.get());
接下去,还要把它放到BpBinder里面去,下次就要使用时,就可以在上一步调用BpBinder::findObj把它找回来了:
- val->attachObject(&gBinderProxyOffsets, refObject,
- jnienv_to_javavm(env), proxy_cleanup);
回到ServiceManager.getIServiceManager中,从下面语句返回:
- sServiceManager = ServiceManagerNative.asInterface(BinderInternal.getContextObject());
相当于是:
- sServiceManager = ServiceManagerNative.asInterface(new BinderProxy());
接下去就是调用ServiceManagerNative.asInterface函数了,这个函数定义在frameworks/base/core/java/android/os/ServiceManagerNative.java文件中:
- public abstract class ServiceManagerNative ......
- {
- ......
- static public IServiceManager asInterface(IBinder obj)
- {
- if (obj == null) {
- return null;
- }
- IServiceManager in =
- (IServiceManager)obj.queryLocalInterface(descriptor);
- if (in != null) {
- return in;
- }
- return new ServiceManagerProxy(obj);
- }
- ......
- }
返回到ServiceManager.getIServiceManager中,从下面语句返回:
- sServiceManager = ServiceManagerNative.asInterface(new BinderProxy());
就相当于是:
- sServiceManager = new ServiceManagerProxy(new BinderProxy());
总结一下,就是在Java层,我们拥有了一个Service Manager远程接口ServiceManagerProxy,而这个ServiceManagerProxy对象在JNI层有一个句柄值为0的BpBinder对象与之通过gBinderProxyOffsets关联起来。
这样获取Service Manager的Java远程接口的过程就完成了。
