- class JavaBBinder : public BBinder
- {
- JavaBBinder(JNIEnv* env, jobject object)
- : mVM(jnienv_to_javavm(env)), mObject(env->NewGlobalRef(object))
- {
- ......
- }
- ......
- virtual status_t onTransact(
- uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags = 0)
- {
- JNIEnv* env = javavm_to_jnienv(mVM);
- ......
- jboolean res = env->CallBooleanMethod(mObject, gBinderOffsets.mExecTransact,
- code, (int32_t)&data, (int32_t)reply, flags);
- ......
- return res != JNI_FALSE ? NO_ERROR : UNKNOWN_TRANSACTION;
- }
- ......
- JavaVM* const mVM;
- jobject const mObject;
- };
前面我们在介绍HelloService的启动过程时,曾经介绍过,JavaBBinder类里面的成员变量mObject就是HelloService类的一个实例对象了。因此,这里通过语句:
- jboolean res = env->CallBooleanMethod(mObject, gBinderOffsets.mExecTransact,
- code, (int32_t)&data, (int32_t)reply, flags);
就调用了HelloService.execTransact函数,而HelloService.execTransact函数继承了Binder类的execTransact函数:
- public class Binder implements IBinder {
- ......
- // Entry point from Android_util_Binder.cpp's onTransact
- private boolean execTransact(int code, int dataObj, int replyObj, int flags) {
- Parcel data = Parcel.obtain(dataObj);
- Parcel reply = Parcel.obtain(replyObj);
- // theoretically, we should call transact, which will call onTransact,
- // but all that does is rewind it, and we just got these from an IPC,
- // so we'll just call it directly.
- boolean res;
- try {
- res = onTransact(code, data, reply, flags);
- } catch (RemoteException e) {
- reply.writeException(e);
- res = true;
- } catch (RuntimeException e) {
- reply.writeException(e);
- res = true;
- } catch (OutOfMemoryError e) {
- RuntimeException re = new RuntimeException("Out of memory", e);
- reply.writeException(re);
- res = true;
- }
- reply.recycle();
- data.recycle();
- return res;
- }
- }
这里又调用了onTransact函数来作进一步处理。由于HelloService类继承了IHelloService.Stub类,而IHelloService.Stub类实现了onTransact函数,HelloService类没有实现,因此,最终调用了IHelloService.Stub.onTransact函数:
- 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
- {
- ......
- @Override
- public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException
- {
- switch (code)
- {
- ......
- case TRANSACTION_getVal:
- {
- data.enforceInterface(DESCRIPTOR);
- int _result = this.getVal();
- reply.writeNoException();
- reply.writeInt(_result);
- return true;
- }
- }
- return super.onTransact(code, data, reply, flags);
- }
- ......
- }
- }
函数最终又调用了HelloService.getVal函数:
- public class HelloService extends IHelloService.Stub {
- ......
- public int getVal() {
- return getVal_native();
- }
- ......
- private static native int getVal_native();
- }
最终,经过层层返回,就回到IHelloService.Stub.Proxy.getVal函数中来了,从下面语句返回:
- mRemote.transact(Stub.TRANSACTION_getVal, _data, _reply, 0);
并将结果读出来:
- _result = _reply.readInt();
最后将这个结果返回到Hello.onClick函数中。
这样,Client通过HelloService的Java远程接口来使用HelloService提供的服务的过程就介绍完了。
至此,Android系统进程间通信Binder机制在应用程序框架层的Java接口源代码分析也完成了,整个Binder机制的学习就结束了。
重新学习Android系统进程间通信Binder机制,请回到Android进程间通信(IPC)机制Binder简要介绍和学习计划一文。
