aidl引用类作为函数的返回值

项目中要编写aidl,为另外一个进程提供本app接受到的一些数据,将这些数据封装为一个对象后作为返回值。

1.aidl对应的目录如下

2.对应的类的代码:

IBufferPlay.aidl

package lenovo.com.ismartvlive.aidl;
//导包
import lenovo.com.ismartvlive.aidl.BufferPlayInfo;

interface IBufferPlay {
   BufferPlayInfo  bufferPlayInfo();
}

AIDLService.java

package lenovo.com.ismartvlive.aidl;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

import lenovo.com.ismartvlive.utils.LogUtil;

public class AIDLService extends Service {
    public AIDLService() {
    }

    private BufferPlayInfo mBufferPlayInfo;

    private final IBinder mBinder = new IServiceProxy();

    @Override
    public IBinder onBind(Intent intent) {
       return mBinder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        init();
        LogUtil.d(this,"onCreate");
    }

    private void init() {
        //TODO 获得信息
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtil.d(this, "onDestroy");
    }

    public BufferPlayInfo getBufferPlayInfo() {
        return mBufferPlayInfo;
    }

    private class IServiceProxy extends IBufferPlay.Stub {
        @Override
        public BufferPlayInfo  bufferPlayInfo(){
            return getBufferPlayInfo();
        }
    }
}

BufferPlayInfo.java 该类要实现parcelable接口,在studio中可以使用插件( android-parcelable-intellij-plugin)生成。

package lenovo.com.ismartvlive.aidl;

import android.os.Parcel;
import android.os.Parcelable;

/**
 * 缓冲实体类
 */
public class BufferPlayInfo implements Parcelable {
    public boolean isBuffer;        //是否在缓冲状态
    public int bufferSchedule;      //缓冲进度
    public double netSpeed;         //网速

    public BufferPlayInfo(boolean isBuffer, int bufferSchedule, double netSpeed) {
        this.isBuffer = isBuffer;
        this.bufferSchedule = bufferSchedule;
        this.netSpeed = netSpeed;
    }


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeByte(isBuffer ? (byte) 1 : (byte) 0);
        dest.writeInt(this.bufferSchedule);
        dest.writeDouble(this.netSpeed);
    }

    protected BufferPlayInfo(Parcel in) {
        this.isBuffer = in.readByte() != 0;
        this.bufferSchedule = in.readInt();
        this.netSpeed = in.readDouble();
    }

    public static final Parcelable.Creator<BufferPlayInfo> CREATOR = new Parcelable.Creator<BufferPlayInfo>() {
        public BufferPlayInfo createFromParcel(Parcel source) {
            return new BufferPlayInfo(source);
        }

        public BufferPlayInfo[] newArray(int size) {
            return new BufferPlayInfo[size];
        }
    };
}

另外还要写一个类对应aidl

BufferInfo.aidl

package lenovo.com.ismartvlive.aidl;
parcelable BufferPlayInfo;//首字母小写

因为要做系统编译,要在android.mk文件中添加:

LOCAL_SRC_FILES += src/lenovo/com/ismartvlive/aidl/IBufferPlay.aidl




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值