您的位置:首页 > 大数据 > 人工智能

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

2016-02-18 17:56 519 查看
项目中要编写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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android aidl