文章目录
AIDL(Android Interface Definition Language) 安卓接口定义语言。
是实现Binder机制,跨进程通讯的一个模板。
定义一个IRemoteService.aidl文件
package com.example.aidldemo.aidltest;
//自定义对象需要手动 import package
import com.example.aidldemo.aidltest.MyData;
interface IRemoteService {
int getPid();
MyData getMyData();
}
自定义对象也需要声明一个aidl文件
package com.example.aidldemo.aidltest;
// Declare any non-default types here with import statements
parcelable MyData;
aidl是面向接口的,重点在于编译器给我们生成的IRemoteService.java文件
/*
* This file is auto-generated. DO NOT MODIFY. 编译后自动生成,勿修改
* Original file: E:\\projects\\aidl_demo\\app\\src\\main\\aidl\\com\\example\\aidldemo\\aidltest\\IRemoteService.aidl
*/
package com.example.aidldemo.aidltest;
public interface IRemoteService extends android.os.IInterface {
/**
* Local-side IPC implementation stub class.
*/
public static abstract class Stub extends android.os.Binder implements com.example.aidldemo.aidltest.IRemoteService {
private static final java.lang.String DESCRIPTOR = "com.example.aidldemo.aidltest.IRemoteService";
/**
* Construct the stub at attach it to the interface.
*/
public Stub() {
this.attachInterface(this, DESCRIPTOR);
}
/**
* Cast an IBinder object into an com.example.aidldemo.aidltest.IRemoteService interface,
* generating a proxy if needed.
*/
public static com.example.aidldemo.aidltest.IRemoteService asInterface(android.os.IBinder obj) {
if ((obj == null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin != null) && (iin instanceof com.example.aidldemo.aidltest.IRemoteService))) {
return ((com.example.aidldemo.aidltest.IRemoteService) iin);
}
return new com.example.aidldemo.aidltest.IRemoteService.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 {
java.lang.String descriptor = DESCRIPTOR;
switch (code) {
case INTERFACE_TRANSACTION: {
reply.writeString(descriptor);
return true;
}
case TRANSACTION_getPid: {
data.enforceInterface(descriptor);
int _result = this.getPid();
reply.writeNoException();
reply.writeInt(_result);
return true;
}
case TRANSACTION_getMyData: {
data.enforceInterface(descriptor);
com.example.aidldemo.aidltest.MyData _result = this.getMyData();
reply.writeNoException();
if ((_result != null)) {
reply.writeInt(1);
_result.writeToParcel(reply, android.os.Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
} else {
reply.writeInt(0);
}
return true;
}
default: {
return super.onTransact(code, data, reply, flags);
}
}
}
private static class Proxy implements com.example.aidldemo.aidltest.IRemoteService {
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 getPid() 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);
mRemote.transact(Stub.TRANSACTION_getPid, _data, _reply, 0);
_reply.readException();
_result = _reply.readInt();
} finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
@Override
public com.example.aidldemo.aidltest.MyData getMyData() throws android.os.RemoteException {
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
com.example.aidldemo.aidltest.MyData _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
mRemote.transact(Stub.TRANSACTION_getMyData, _data, _reply, 0);
_reply.readException();
if ((0 != _reply.readInt())) {
_result = com.example.aidldemo.aidltest.MyData.CREATOR.createFromParcel(_reply);
} else {
_result = null;
}
} finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
}
static final int TRANSACTION_getPid = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
static final int TRANSACTION_getMyData = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
}
public int getPid() throws android.os.RemoteException;
public com.example.aidldemo.aidltest.MyData getMyData() throws android.os.RemoteException;
}