RemoteSever的通信AIDL

来源:互联网 发布:环保75标准数据 编辑:程序博客网 时间:2024/06/06 03:06
1.在main文件中创建一个AIDL文件夹,并在文件中创建ICalcInterface.aidl 文件,注意当在另外一个应用程序中也要创建一个一模一样的文件(该文件的所存在的位置,即包名+文件名),点击重建工程,此时工程文件中产生一个generated文件夹。
// ICalcInterface.aidlpackage com.wwj_fly.remoteservicedemo.services;import com.wwj_fly.remoteservicedemo.model.Person;// Declare any non-default types here with import statementsinterface ICalcInterface {   // 定义AIDL中提供的接口 想当于告诉其他应用程序 可以调用的方法   /*   *   * 对外定义一个 add 的方法 支持 两个参数 一个返回值  */   int add(int a,int b);   int subtraction(int a,int b);   //当参数为数组的时候 需要指定数据的方向   // in  :代表客户端直接给服务 传递数据,   // out : 代表服务可以直接操作数据 并且 客户端可以接收 修改   // inout: 代表客户端 可以传递数据 并且服务可以修改数组 返回给客户端   String hexEncode(in byte[] data);/*    使用复杂的数据对象 进行传递*/   String processRect(in Rect rect);   // 使用自定义对象 传递   void testPerson(in Person p);}
Person.aidl
// Person.aidlpackage com.wwj_fly.remoteservicedemo.model;// Declare any non-default types here with import statements// 声明Person 可以被多个程序相互传递parcelable Person;


generated文件夹

代码:
/* * This file is auto-generated.  DO NOT MODIFY. * Original file: D:\\AndroidstudioCode\\RemoteServiceDemo\\app\\src\\main\\aidl\\com\\wwj_fly\\remoteservicedemo\\services\\ICalcInterface.aidl */package com.wwj_fly.remoteservicedemo.services;// Declare any non-default types here with import statementspublic interface ICalcInterface extends android.os.IInterface{/** Local-side IPC implementation stub class. */public static abstract class Stub extends android.os.Binder implements com.wwj_fly.remoteservicedemo.services.ICalcInterface{private static final java.lang.String DESCRIPTOR = "com.wwj_fly.remoteservicedemo.services.ICalcInterface";/** Construct the stub at attach it to the interface. */public Stub(){this.attachInterface(this, DESCRIPTOR);}/** * Cast an IBinder object into an com.wwj_fly.remoteservicedemo.services.ICalcInterface interface, * generating a proxy if needed. */public static com.wwj_fly.remoteservicedemo.services.ICalcInterface asInterface(android.os.IBinder obj){if ((obj==null)) {return null;}android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);if (((iin!=null)&&(iin instanceof com.wwj_fly.remoteservicedemo.services.ICalcInterface))) {return ((com.wwj_fly.remoteservicedemo.services.ICalcInterface)iin);}return new com.wwj_fly.remoteservicedemo.services.ICalcInterface.Stub.Proxy(obj);}@Override 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_add:{data.enforceInterface(DESCRIPTOR);int _arg0;_arg0 = data.readInt();int _arg1;_arg1 = data.readInt();int _result = this.add(_arg0, _arg1);reply.writeNoException();reply.writeInt(_result);return true;}case TRANSACTION_subtraction:{data.enforceInterface(DESCRIPTOR);int _arg0;_arg0 = data.readInt();int _arg1;_arg1 = data.readInt();int _result = this.subtraction(_arg0, _arg1);reply.writeNoException();reply.writeInt(_result);return true;}case TRANSACTION_hexEncode:{data.enforceInterface(DESCRIPTOR);byte[] _arg0;_arg0 = data.createByteArray();java.lang.String _result = this.hexEncode(_arg0);reply.writeNoException();reply.writeString(_result);return true;}case TRANSACTION_processRect:{data.enforceInterface(DESCRIPTOR);android.graphics.Rect _arg0;if ((0!=data.readInt())) {_arg0 = android.graphics.Rect.CREATOR.createFromParcel(data);}else {_arg0 = null;}java.lang.String _result = this.processRect(_arg0);reply.writeNoException();reply.writeString(_result);return true;}case TRANSACTION_testPerson:{data.enforceInterface(DESCRIPTOR);com.wwj_fly.remoteservicedemo.model.Person _arg0;if ((0!=data.readInt())) {_arg0 = com.wwj_fly.remoteservicedemo.model.Person.CREATOR.createFromParcel(data);}else {_arg0 = null;}this.testPerson(_arg0);reply.writeNoException();return true;}}return super.onTransact(code, data, reply, flags);}private static class Proxy implements com.wwj_fly.remoteservicedemo.services.ICalcInterface{private android.os.IBinder mRemote;Proxy(android.os.IBinder remote){mRemote = remote;}@Override public android.os.IBinder asBinder(){return mRemote;}public java.lang.String getInterfaceDescriptor(){return DESCRIPTOR;}@Override public int add(int a, int b) 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);_data.writeInt(a);_data.writeInt(b);mRemote.transact(Stub.TRANSACTION_add, _data, _reply, 0);_reply.readException();_result = _reply.readInt();}finally {_reply.recycle();_data.recycle();}return _result;}@Override public int subtraction(int a, int b) 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);_data.writeInt(a);_data.writeInt(b);mRemote.transact(Stub.TRANSACTION_subtraction, _data, _reply, 0);_reply.readException();_result = _reply.readInt();}finally {_reply.recycle();_data.recycle();}return _result;}//当参数为数组的时候 需要指定数据的方向// in  :代表客户端直接给服务 传递数据,// out : 代表服务可以直接操作数据 并且 客户端可以接收 修改// inout: 代表客户端 可以传递数据 并且服务可以修改数组 返回给客户端@Override public java.lang.String hexEncode(byte[] data) throws android.os.RemoteException{android.os.Parcel _data = android.os.Parcel.obtain();android.os.Parcel _reply = android.os.Parcel.obtain();java.lang.String _result;try {_data.writeInterfaceToken(DESCRIPTOR);_data.writeByteArray(data);mRemote.transact(Stub.TRANSACTION_hexEncode, _data, _reply, 0);_reply.readException();_result = _reply.readString();}finally {_reply.recycle();_data.recycle();}return _result;}/*    使用复杂的数据对象 进行传递*/@Override public java.lang.String processRect(android.graphics.Rect rect) throws android.os.RemoteException{android.os.Parcel _data = android.os.Parcel.obtain();android.os.Parcel _reply = android.os.Parcel.obtain();java.lang.String _result;try {_data.writeInterfaceToken(DESCRIPTOR);if ((rect!=null)) {_data.writeInt(1);rect.writeToParcel(_data, 0);}else {_data.writeInt(0);}mRemote.transact(Stub.TRANSACTION_processRect, _data, _reply, 0);_reply.readException();_result = _reply.readString();}finally {_reply.recycle();_data.recycle();}return _result;}// 使用自定义对象 传递@Override public void testPerson(com.wwj_fly.remoteservicedemo.model.Person p) throws android.os.RemoteException{android.os.Parcel _data = android.os.Parcel.obtain();android.os.Parcel _reply = android.os.Parcel.obtain();try {_data.writeInterfaceToken(DESCRIPTOR);if ((p!=null)) {_data.writeInt(1);p.writeToParcel(_data, 0);}else {_data.writeInt(0);}mRemote.transact(Stub.TRANSACTION_testPerson, _data, _reply, 0);_reply.readException();}finally {_reply.recycle();_data.recycle();}}}static final int TRANSACTION_add = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);static final int TRANSACTION_subtraction = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);static final int TRANSACTION_hexEncode = (android.os.IBinder.FIRST_CALL_TRANSACTION + 2);static final int TRANSACTION_processRect = (android.os.IBinder.FIRST_CALL_TRANSACTION + 3);static final int TRANSACTION_testPerson = (android.os.IBinder.FIRST_CALL_TRANSACTION + 4);}public int add(int a, int b) throws android.os.RemoteException;public int subtraction(int a, int b) throws android.os.RemoteException;//当参数为数组的时候 需要指定数据的方向// in  :代表客户端直接给服务 传递数据,// out : 代表服务可以直接操作数据 并且 客户端可以接收 修改// inout: 代表客户端 可以传递数据 并且服务可以修改数组 返回给客户端public java.lang.String hexEncode(byte[] data) throws android.os.RemoteException;/*    使用复杂的数据对象 进行传递*/public java.lang.String processRect(android.graphics.Rect rect) throws android.os.RemoteException;// 使用自定义对象 传递public void testPerson(com.wwj_fly.remoteservicedemo.model.Person p) throws android.os.RemoteException;}
CalcService.java
public class CalcService extends Service {    private static final String TAG = "CalcService";    public CalcService() {    }    @Override    public void onCreate() {        super.onCreate();        Log.d(TAG, "onCreate: called");    }    public class Calculator extends ICalcInterface.Stub{        public int add(int a,int b){            return  a + b;        }        public int subtraction(int a, int b){            return a - b;        }        @Override        public String hexEncode(byte[] data) throws RemoteException {            String ret = null;            if (data != null) {                ret = "len:" + data.length;            }else {                ret ="len 0";            }            // 如果执行以下语句 那么数据方向应该包括inout            // 1.Parcelable 接口            // 2.            return ret;        }        @Override        public String processRect(Rect rect) throws RemoteException {            return "test";        }        @Override        public void testPerson(Person p) throws RemoteException {        }    }    @Override    public IBinder onBind(Intent intent) {        Log.d(TAG, "onBind() called with: " + "intent = [" + intent + "]");        return new Calculator();    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        Log.d(TAG, "onStartCommand() called with: " + "intent = [" + intent + "], flags = [" + flags + "], startId = [" + startId + "]");        return super.onStartCommand(intent, flags, startId);    }    @Override    public void onDestroy() {        Log.d(TAG, "onDestroy() called with: " + "");        super.onDestroy();    }}
2.创建appClient应用程序
设置的AIDL文件存储路径,如图:
appClient应用程序的UI线程
public class MainActivity extends AppCompatActivity implements ServiceConnection {    private ICalcInterface mCalcInterface;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        // 绑定远程服务        Intent intent = new Intent();        intent.setClassName("com.wwj_fly.remoteservicedemo",                "com.wwj_fly.remoteservicedemo.services.CalcService"        );        bindService(intent,this,BIND_AUTO_CREATE);    }    public void btnStartService(View view) {        Intent intent = new Intent();        // 使用设置类名的形式 可以调用其他程序的服务        // 参数1: String 远程程序的 applicationId        // 参数2: 要启动的服务类的全名        intent.setClassName("com.wwj_fly.remoteservicedemo",                "com.wwj_fly.remoteservicedemo.services.CalcService"        );        startService(intent);    }    //    @Override    public void onServiceConnected(ComponentName name, IBinder service) {        //AIDL 生成的接口 提供了一个特定的 可以讲IBinder 转换为接口对象的方法        mCalcInterface = ICalcInterface.Stub.asInterface(service);        try {            int sum = mCalcInterface.add(2, 4);            int subtraction = mCalcInterface.subtraction(2, 4);            Log.d("onServiceConnected", "sum"+ sum);            Log.d("onServiceConnected", "subtraction"+ subtraction);        } catch (RemoteException e) {            e.printStackTrace();        }    }    @Override    public void onServiceDisconnected(ComponentName name) {    }}
Person.java
public class Person implements Parcelable {    public static final Creator<Person> CREATOR =            new Creator<Person>() {                /*                * 反序列化 从 Parcel source 中获取对象                *                * */                @Override                public Person createFromParcel(Parcel source) {                    Person ret = new Person();                    ret.name = source.readString();                    ret.age = source.readInt();                    ret.address = source.readString();                    return ret;                }                /*                * 创建多个对象                * */                @Override                public Person[] newArray(int size) {                    return new Person[size];                }            };    /*    *代表当前类包含几个特殊的对象类型    * 默认没有特殊对象 返回0    * */    @Override    public int describeContents() {        return 0;    }    /*    * 序列化输出部分    * Parcel dest 可以认为是输出流    * */    @Override    public void writeToParcel(Parcel dest, int flags) {            dest.writeString(name);            dest.writeInt(age);            dest.writeString(address);    }    // ----------------------------    private String name;    private int age;    private String address;    public Person(){    }    public String getAddress() {        return address;    }    public void setAddress(String address) {        this.address = address;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }}

这样我们就可以通过appClient应用程序来访问app的应用程序了。
                                             
0 0
原创粉丝点击