基于binder机制编写aidl接口文件

基于binder机制编写aidl接口文件

/**
 * Created by liyihang on 17-7-10.
 */

public interface MyUserInterface extends IInterface {

    String getFont(String msg) throws RemoteException;
    int getNums(int[] arr) throws RemoteException;

    public static abstract class Stub extends Binder implements MyUserInterface{

        private static final String DESCRIPTOR = "com.asinterface.bins.MyUserInterface";
        private static final int TRANSALTE_ID=IBinder.FIRST_CALL_TRANSACTION+0;
        private static final int TRANSALTE_ID_1=IBinder.FIRST_CALL_TRANSACTION+1;


        public Stub() {
	    //attach 自定义接口
            this.attachInterface(this, DESCRIPTOR);
        }

        @Override
        public IBinder asBinder() {
	    //返回传递binder对象
            return this;
        }

        public static MyUserInterface asInterface(IBinder iBinder){
	    //获取代理接口的方法
            if (iBinder==null)
            {
                return null;
            }
            IInterface iInterface = iBinder.queryLocalInterface(DESCRIPTOR);
            if (iInterface!=null && iInterface instanceof MyUserInterface)
            {
                return (MyUserInterface) iInterface;
            }
            return new Proxy(iBinder);
        }

        @Override
        protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException {
	    //service 服务端回调
            switch (code)
            {

                case INTERFACE_TRANSACTION:
                {
                    reply.writeString(DESCRIPTOR);
                    return true;
                }

                case TRANSALTE_ID:
                {
                    data.enforceInterface(DESCRIPTOR);
                    String s = data.readString();
                    String font = getFont(s);
                    reply.writeNoException();
                    reply.writeString(font);
                    return true;
                }

                case TRANSALTE_ID_1:
                {
                    data.enforceInterface(DESCRIPTOR);
                    int[] arr=new int[100000];
                    data.readIntArray(arr);
                    int font = getNums(arr);
                    reply.writeNoException();
                    reply.writeInt(font);
                    return true;
                }
            }
            return super.onTransact(code, data, reply, flags);
        }
	// 客户端代理接口
        public static class Proxy implements MyUserInterface{

            private IBinder iBinder;

            public Proxy(IBinder iBinder) {
                this.iBinder = iBinder;
            }

            @Override
            public String getFont(String msg) throws RemoteException {
                Parcel data = Parcel.obtain();
                Parcel reply = Parcel.obtain();
                String result=null;
                try {
                    data.writeInterfaceToken(DESCRIPTOR);
                    data.writeString(msg);
                    iBinder.transact(TRANSALTE_ID, data, reply, 0);
                    reply.readException();
                    result = reply.readString();
                }finally {
                    reply.recycle();
                    data.recycle();
                }
                return result;
            }

            @Override
            public int getNums(int[] arr) throws RemoteException {
                Parcel data = Parcel.obtain();
                Parcel reply = Parcel.obtain();
                int result=0;
                try {
                    data.writeInterfaceToken(DESCRIPTOR);
                    data.writeIntArray(arr);
                    iBinder.transact(TRANSALTE_ID_1, data, reply, 0);
                    reply.readException();
                    result = reply.readInt();
                }finally {
                    reply.recycle();
                    data.recycle();
                }
                return result;
            }

            @Override
            public IBinder asBinder() {
                return iBinder;
            }
        }

    }

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值