基于binder机制编写aidl接口文件
来源:互联网 发布:淘宝店铺花呗怎么开通 编辑:程序博客网 时间:2024/05/18 13:11
基于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
- 基于binder机制编写aidl接口文件
- Binder机制和AIDL
- AIDL 与 Binder机制
- 基于aidl机制的binder连接池的实现
- binder与aidl机制区别
- Android aidl Binder 机制学习
- 实现AIDL接口的Binder连接池
- 使用AIDL分析Binder的工作机制
- Android——Binder(AIDL)机制
- AIDL下Binder的工作机制
- AIDL使用和Binder机制详解
- 通过 AIDL 分析 Binder 的工作机制
- Android aidl 案例分析Binder机制
- Binder底层原理,aidl.java文件分析
- Binder AIDL
- Binder&AIDL
- 从AIDL开始谈Android进程间Binder通信机制
- 从AIDL开始谈Android进程间Binder通信机制
- Unity3D 编写游戏运行时间 格式为00:00:00
- [bzoj3653]谈笑风生 主席树
- Java 类加载机制
- 关于对Tarjan算法的思考与尝试
- Java集合-常用集合类
- 基于binder机制编写aidl接口文件
- MySQL中触发器
- spring框架学习(三)junit单元测试
- LoadRunner实战小例子(在线预订机票)
- PL/SQL
- An in-depth look into the ARM virtualization extensions
- 知识记录
- shopnc学习起始
- ext2文件系统