这里首先把Java语言实现的Parcel对象class转换成C++语言实现的Parcel对象parcel,接着,通过parcel->readStrongBinder函数来获得一个Binder引用。
我们在前面学习Binder机制时,在Android系统进程间通信(IPC)机制Binder中的Client获得Server远程接口过程源代码分析这篇文章中,曾经分析过这个函数,它最终返回来的是一个BpBinder对象,因此,下面的语句:
- return javaObjectForIBinder(env, parcel->readStrongBinder());
就相当于是:
- return javaObjectForIBinder(env, new BpBinder(handle));
这里的handle就是HelloService这个Binder实体在Client进程中的句柄了,它是由Binder驱动程序设置的,上层不用关心它的值具体是多少。至于javaObjectForIBinder这个函数,我们前面介绍如何获取Service Manager的Java远程接口时已经有详细介绍,这里就不累述了,它的作用就是创建一个BinderProxy对象,并且把刚才获得的BpBinder对象的地址保存在这个BinderProxy对象的mObject成员变量中。
最后返回到Hello.onCreate函数中,从下面语句返回:
- helloService = IHelloService.Stub.asInterface(
- ServiceManager.getService("hello"));
就相当于是:
- helloService = IHelloService.Stub.asInterface(new BinderProxy()));
回忆一下前面介绍IHelloService接口的定义时,IHelloService.Stub.asInterface是这样定义的:
- public interface IHelloService extends Android.os.IInterface
- {
- /** Local-side IPC implementation stub class. */
- public static abstract class Stub extends android.os.Binder implements android.os.IHelloService
- {
- ......
- public static android.os.IHelloService asInterface(android.os.IBinder obj)
- {
- if ((obj==null)) {
- return null;
- }
- android.os.IInterface iin = (android.os.IInterface)obj.queryLocalInterface(DESCRIPTOR);
- if (((iin!=null)&&(iin instanceof android.os.IHelloService))) {
- return ((android.os.IHelloService)iin);
- }
- return new android.os.IHelloService.Stub.Proxy(obj);
- }
- ......
- }
- }
这里的obj是一个BinderProxy对象,它的queryLocalInterface返回null,于是调用下面语句获得HelloService的远程接口:
- return new android.os.IHelloService.Stub.Proxy(obj);
相当于是:
- return new android.os.IHelloService.Stub.Proxy(new BinderProxy());
这样,我们就获得了HelloService的远程接口了,它实质上是一个实现了IHelloService接口的IHelloService.Stub.Proxy对象。
五. Client通过HelloService的Java远程接口来使用HelloService提供的服务的过程
上面介绍的Hello这个Activity获得了HelloService的远程接口后,就可以使用它的服务了。
我们以使用IHelloService.getVal函数为例详细说明。在Hello::onClick函数中调用了IHelloService.getVal函数:
- public class Hello extends Activity implements OnClickListener {
- ......
- @Override
- public void onClick(View v) {
- if(v.equals(readButton)) {
- int val = helloService.getVal();
- ......
- }
- else if(v.equals(writeButton)) {
- ......
- }
- else if(v.equals(clearButton)) {
- ......
- }
- }
- ......
- }
通知前面的分析,我们知道,这里的helloService接口实际上是一个IHelloService.Stub.Proxy对象,因此,我们进入到IHelloService.Stub.Proxy类的getVal函数中:
- public interface IHelloService extends android.os.IInterface
- {
- /** Local-side IPC implementation stub class. */
- public static abstract class Stub extends android.os.Binder implements android.os.IHelloService
- {
- ......
- private static class Proxy implements android.os.IHelloService
- {
- private android.os.IBinder mRemote;
- ......
- public int getVal() throws android.os.RemoteException
- {
- android.os.Parcel _data = android.os.Parcel.obtain();
- android.os.Parcel _reply = android.os.Parcel.obtain();
- int _result;
- try {
- _data.writeInterfaceToken(DESCRIPTOR);
- mRemote.transact(Stub.TRANSACTION_getVal, _data, _reply, 0);
- _reply.readException();
- _result = _reply.readInt();
- }
- finally {
- _reply.recycle();
- _data.recycle();
- }
- return _result;
- }
- }
- ......
- static final int TRANSACTION_getVal = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
- }
- ......
- }
这里我们可以看出,实际上是通过mRemote.transact来请求HelloService执行TRANSACTION_getVal操作。这里的mRemote是一个BinderProxy对象,这是我们在前面获取HelloService的Java远程接口的过程中创建的。
