美文网首页
AIDL 使用步骤

AIDL 使用步骤

作者: that_is_this | 来源:发表于2018-07-31 17:37 被阅读10次

1. 常规参数的

  1. 右键 new -> AIDL ,name - > myAIDL
  2. Build -> Make Project
  3. bindService 函数启动 Service 进程
  4. 重写服务类的 onBind 函数
  5. 新建 myBinder 类并继承 myAIDL.Stub()
  6. 重写 AIDL 的接口函数
  7. 重写 onServiceConnected() 函数
  8. 获取 myAIDL 类对象,= myAIDL.Stub.asInterface(iBinder)
  9. 使用类对象就可以调用对应的接口函数

服务类:

    @Override
    public IBinder onBind(Intent arg0) {
        mContext = this;
        pkgName = mContext.getPackageName();
    return iBinder;
    }
    public class DUBinder extends myAIDL.Stub {
            String dosmThing() {
                 log"123"; 
              }
        }

myAIDL.aidl 类:

 String dosmThing();

mainActivity 类:

    private myAIDL myAIDL;

    ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            try {
                myAIDL = ServiceManager.Stub.asInterface(iBinder);
                myAIDL .dosmThing();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
        }
    };

2. 类参数

对于 AIDL 的参数,有需要传入类对象的,这个类需要继承 implements Parcelable 类
示例如下:

public class DUVCell implements Parcelable {

    public int type;
    public int mcc;
    public int mnc;
    public int psc;
    public int lac;
    public int cid;
    public int baseStationId;
    public int systemId;
    public int networkId;


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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(this.type);
        dest.writeInt(this.mcc);
        dest.writeInt(this.mnc);
        dest.writeInt(this.psc);
        dest.writeInt(this.lac);
        dest.writeInt(this.cid);
        dest.writeInt(this.baseStationId);
        dest.writeInt(this.systemId);
        dest.writeInt(this.networkId);
    }

    public DUVCell() {
    }

    public DUVCell(Parcel in) {
        this.type = in.readInt();
        this.mcc = in.readInt();
        this.mnc = in.readInt();
        this.psc = in.readInt();
        this.lac = in.readInt();
        this.cid = in.readInt();
        this.baseStationId = in.readInt();
        this.systemId = in.readInt();
        this.networkId = in.readInt();
    }

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

        @Override
        public DUVCell[] newArray(int size) {
            return new DUVCell[size];
        }
    };
}

在声明了这个类后,就可以在 import 相应的类后传入对应的参数。同时需要把这个类进行 AIDL 处理,并且要对应包路径:

// ParcelTest.aidl
package com.saliey.twobutton;

// Declare any non-default types here with import statements

parcelable ParcelTest;

相关文章

网友评论

      本文标题:AIDL 使用步骤

      本文链接:https://www.haomeiwen.com/subject/doihvftx.html