1、IBinder接口定义:
public String getInterfaceDescriptor();
public boolean pingBinder();
public boolean isBinderAlive();
public IInterface queryLocalInterface(String descriptor);
public void dump(FileDescriptor fd, String[] args)
public boolean transact(int code, Parcel data, Parcel reply, int flags);
public void linkToDeath(DeathRecipient recipient, int flags);
public boolean unlinkToDeath(DeathRecipient recipient, int flags);
2、IInterface接口定义:
public IBinder asBinder();
3、aidl接口文件定义。和普通的接口定义类似,只不过最终文件的扩展名是aidl
interface IUpgradeLocalService {
boolean searchLocalFile(String path);
}
4、服务端使用代码如下:
private IUpgradeLocalService.Stub mStub = new IUpgradeLocalService.Stub() {
public boolean searchLocalFile(final String path) {
// TODO:do something
return true;
}
}
5、客户端使用代码如下:
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceDisconnected(ComponentName name) {
mService = null;
}
public void onServiceConnected(ComponentName name, IBinder service) {
try {
mService = IUpgradeLocalService.Stub.asInterface(service);
// TODO: do something
} catch (RemoteException e) {
e.printStackTrace();
}
}
};
6、aidl自动生成的对应的java文件。该文件定义了一个接口,该接口扩展自IInterface接口。
public interface IUpgradeLocalService extends android.os.IInterface {
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements
com.amlogic.netfilebrowser.IUpgradeLocalService {
private static final java.lang.String DESCRIPTOR = "com.amlogic.netfilebrowser.IUpgradeLocalService";
/** Construct the stub at attach it to the interface. */
public Stub() {
this.attachInterface(this, DESCRIPTOR);
}
/**
* Cast an IBinder object into an
* com.amlogic.netfilebrowser.IUpgradeLocalService interface, generating
* a proxy if needed.
*/
public static com.amlogic.netfilebrowser.IUpgradeLocalService asInterface(
android.os.IBinder obj) {
if ((obj == null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin != null) && (iin instanceof com.amlogic.netfilebrowser.IUpgradeLocalService))) {
return ((com.amlogic.netfilebrowser.IUpgradeLocalService) iin);
}
return new com.amlogic.netfilebrowser.IUpgradeLocalService.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 {
switch (code) {
case INTERFACE_TRANSACTION: {
reply.writeString(DESCRIPTOR);
return true;
}
case TRANSACTION_searchLocalFile: {
data.enforceInterface(DESCRIPTOR);
java.lang.String _arg0;
_arg0 = data.readString();
boolean _result = this.searchLocalFile(_arg0);
reply.writeNoException();
reply.writeInt(((_result) ? (1) : (0)));
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
private static class Proxy implements
com.amlogic.netfilebrowser.IUpgradeLocalService {
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 boolean searchLocalFile(java.lang.String path)
throws android.os.RemoteException {
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
boolean _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeString(path);
mRemote.transact(Stub.TRANSACTION_searchLocalFile, _data,
_reply, 0);
_reply.readException();
_result = (0 != _reply.readInt());
} finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
}
static final int TRANSACTION_searchLocalFile = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
}
public boolean searchLocalFile(java.lang.String path)
throws android.os.RemoteException;
}
6、客户端在Context.bindService()的回调中,使用IUpgradeLocalService.Stub.asInterface(IBinder b)得到一个IUpgradeLocalService的内部类Proxy对象。注意,参数IBinder最终保存为Proxy的mRemote变量。调用Proxy的方法,则最终调用到了mRemote的transact()方法。该方法通过binder机制,到了服务端IUpgradeLocalService.Stub的onTransact()方法,最终调用到了Stub的自定义方法里。