aidl传递boolean数据byte数组

来源:互联网 发布:职场女性知乎 编辑:程序博客网 时间:2024/06/06 04:19

要解决什么问题

1、aidl怎么传递boolean参数?Parcel中是否有writeBoolean方法?如果没有怎么解决?

没有writeBoolean,不能直接传递boolean.
客户端传递byte数据,服务端解出来,转换为boolean:

#客户端parcel_C.writeByte((byte) (booleanValue? 1 : 0));  #服务端parcel_S.readByte()!= 0

2、传递一个byte数组要怎么做?

#客户端parcel_C.writeInt(array.length);parcel_C.writeByteArray(array);
#服务端byte[] _byte = new byte[source.readInt()]; source.readByteArray(_byte);

3、in、inout、inout是怎么用的?
in表示从客户端到服务器
out表示服务器到客户端

package com.example.aidl;interface IBase{ void foo(in boolean login);}

实例

接下来我们演示一个例子。aidl传入一个boolean类型参数,同时返回一个boolean和一个byte[](不引入新类)。

分析

返回值是一个boolean和一个byte数组,显然不引入新类,那就只能用byte数组存放了。我们可以把byte[0]的值设置为(byte) (booleanValue? 1 : 0)。

代码分三步

1、定义ADIL接口定义 2、客户端代码 3、服务器代码

定义ADIL接口

package com.example.aidl;interface IBase{     byte[] foo2(in boolean login);}

客户端代码

    /**     * 传入boolean返回byte[]     */    public void foo2() {        if(iBase==null)            return;        try {            boolean sentBoolean=true;            byte[] result=iBase.foo2(sentBoolean);            boolean backBoolean =            Toast.makeText(getApplicationContext(),            "发送boolean:"            +sentBoolean+ " 返回 boolean:"+backBoolean,             Toast.LENGTH_SHORT).show();        } catch (RemoteException e) {            e.printStackTrace();        }    }    IBase iBase;    /**     * 连接AIDL     */    public void Connect()    {        bindService(new Intent("com.service.use"),        serviceConnection,Context.BIND_AUTO_CREATE);    }    /**     * 连接类实现     */    ServiceConnection serviceConnection=    new ServiceConnection() {        @Override        public void onServiceDisconnected(ComponentName         name) {            iBase=null;            Toast.makeText(MainActivity.this,            "连接断开",            Toast.LENGTH_SHORT).show();        }        @Override        public void onServiceConnected(ComponentName name,         IBinder service) {            iBase=IBase.Stub.asInterface(service);            Toast.makeText(MainActivity.this,            "连接成功",            Toast.LENGTH_SHORT).show();        }    };

服务器代码

public class AIDLService extends Service{    @Override    public IBinder onBind(Intent intent) {        return stub;    }    private IBase.Stub stub=new IBase.Stub() {              @Override        public byte[] foo2(boolean booleanValue) throws         RemoteException {            System.out.println("线程--"            +Thread.currentThread().getName());            System.out.println("客户端发送"+booleanValue);            //要返回的boolean            boolean backBoolean=false;            //要返回的byte数组            byte[] original = new byte[] { 2, 3, 4, 5 };            //组合            byte[] newArray = Arrays.copyOf(original,            original.length + 1);            newArray[newArray.length-1]=            (byte) (backBoolean? 1 : 0);            return newArray;        }    };}

完整demo

package com.example.aidl;import com.example.aidl.UserInfo;import com.example.aidl.UserData;interface IBase{     int    add(int i,int j);     String getUserInfo(in UserInfo userinfo);     void   getaList(out String[] list);       void   setaList(in String[] list);     void   gettList(inout String[] list);     UserData foo(in boolean login);     byte[] foo2(in boolean login);}

in、out、inout怎么用,怎么发送对象到服务器,怎么返回对象到客户端

demo下载

https://github.com/linuxhsj/adil