美文网首页
Java 服务接口的定义和解析

Java 服务接口的定义和解析

作者: ColdWave | 来源:发表于2018-07-21 17:09 被阅读0次

    Java 通过 Android 接口描述语言(Android Interface Definition Language, AIDL) 来定义 Java 服务接口.

    // framework/base/core/java/android/os/IFregService.aidl
    package android.os;
    
    interface IFregService {
        void setVal(int val);
        int getVal();
    }
    

    aidl 文件编译后会生成一个中间文件 IFregService.java

    /*
     * This file is auto-generated.  DO NOT MODIFY.
     * Original file: frameworks/base/core/java/android/os/IFregService.aidl
     */
    package android.os;
     
    public interface IFregService extends android.os.IInterface {
        /** Local-side IPC implementation stub class. */
        public static abstract class Stub extends android.os.Binder implements
                android.os.IFregService {
            private static final java.lang.String DESCRIPTOR = "android.os.IFregService";
     
            /** Construct the stub at attach it to the interface. */
            public Stub() {
                this.attachInterface(this, DESCRIPTOR);
            }
     
            /**
             * Cast an IBinder object into an android.os.IFregService interface,
             * generating a proxy if needed.
             */
            public static android.os.IFregService 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.IFregService))) {
                    return ((android.os.IFregService) iin);
                }
                return new android.os.IFregService.Stub.Proxy(obj);
            }
     
            public android.os.IBinder asBinder() {
                return this;
            }
     
            @Override
            public boolean onTransact(int code, android.os.Parcel data,
                    android.os.Parcel reply, int flags)
                    throws android.os.RemoteException {
                switch (code) {
                case INTERFACE_TRANSACTION: {
                    reply.writeString(DESCRIPTOR);
                    return true;
                }
                case TRANSACTION_setVal: {
                    data.enforceInterface(DESCRIPTOR);
                    int _arg0;
                    _arg0 = data.readInt();
                    this.setVal(_arg0);
                    reply.writeNoException();
                    return true;
                }
                case TRANSACTION_getVal: {
                    data.enforceInterface(DESCRIPTOR);
                    int _result = this.getVal();
                    reply.writeNoException();
                    reply.writeInt(_result);
                    return true;
                }
                }
                return super.onTransact(code, data, reply, flags);
            }
     
            private static class Proxy implements android.os.IFregService {
                private android.os.IBinder mRemote;
     
                Proxy(android.os.IBinder remote) {
                    mRemote = remote;
                }
     
                public android.os.IBinder asBinder() {
                    return mRemote;
                }
     
                public java.lang.String getInterfaceDescriptor() {
                    return DESCRIPTOR;
                }
     
                public void setVal(int val) throws android.os.RemoteException {
                    android.os.Parcel _data = android.os.Parcel.obtain();
                    android.os.Parcel _reply = android.os.Parcel.obtain();
                    try {
                        _data.writeInterfaceToken(DESCRIPTOR);
                        _data.writeInt(val);
                        mRemote.transact(Stub.TRANSACTION_setVal, _data, _reply, 0);
                        _reply.readException();
                    } finally {
                        _reply.recycle();
                        _data.recycle();
                    }
                }
     
                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_setVal = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
            static final int TRANSACTION_getVal = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
        }
     
        public void setVal(int val) throws android.os.RemoteException;
     
        public int getVal() throws android.os.RemoteException;
    }
    
    • IFregService interface
    • 内部抽象类: IFregService.Stub
    • 内部抽象类: IFregService.Stub.Proxy

    IFregService.Stub 和 IFregService.Stub.Proxy 都实现了 IFregService 接口。

    • IFregService.Stub 描述一个 Java 服务
    • IFregService.Stub.proxy 描述这个 Java 服务的代理对象。

    我们要实现的是服务是以 IFregService.Stub 为父类,并且实现 setVal 和 getVal 方法。

    • IFregService.Stub 的 asBinder 方法,是将 BinderProxy 封装成 IFregService.Stub.Proxy 对象。
    • IFregService.Stub 的 onTransact 方法用来接收和分发进程间通信请求。当 Client 进程使用 通过 Proxy 发送 TRANSACTION_setVal, TRANSACTION_getVal 的通信请求时,IFregService.Stub 的onTransact 就会被调用。
    • IFregService.Stub.Proxy 内部成员变量 mRemote,指向了 JavaBinderProxy。

    相关文章

      网友评论

          本文标题:Java 服务接口的定义和解析

          本文链接:https://www.haomeiwen.com/subject/pcnhmftx.html