/********************************************************************************
* Android AIDL自动生成Java文件测试
* 说明:
* 知道有aidl这东西已经挺久了,但是一直没有花时间来系统了解一下其工作机制,现在
* 花点时间一点一点验证一下其功能。
*
* 2016-5-8 深圳 南山平山村 曾剑锋
*******************************************************************************/一、参考文章:1. Android AIDL使用详解
http://blog.youkuaiyun.com/stonecao/article/details/6425019
2. android进程间通信:使用AIDL
http://blog.youkuaiyun.com/saintswordsman/article/details/5130947
二、cat forService.aidl
package com.example.aidltest.aidl;
import com.example.aidltest.aidl.forActivity;interfaceforService {voidregisterTestCall(forActivity cb);voidinvokCallBack();
}
三、cat forService.java/** This file is auto-generated. DO NOT MODIFY.
* Original file: D:\\soft\\Android\\workspace\\AidlTest\\src\\com\\example\\aidltest\\aidl\\forService.aidl*/package com.example.aidltest.aidl;//继承了android.os.IInterface接口
public interfaceforService extends android.os.IInterface {/** Local-side IPC implementation stub class.*/
//Stub类继承了Binder,并继承我们在aidl文件中定义的接口
public static abstract classStub extends android.os.Binder implements
com.example.aidltest.aidl.forService {private static final java.lang.String DESCRIPTOR = "com.example.aidltest.aidl.forService";/** Construct the stub at attach it to the interface.*/
publicStub() {this.attachInterface(this, DESCRIPTOR);
}/**
* Cast an IBinder object into an com.example.aidltest.aidl.forService
* interface, generating a proxy if needed.*/
//在使用的时候经常会看到这一部分的内容
public staticcom.example.aidltest.aidl.forService asInterface(
android.os.IBinder obj) {if ((obj == null)) {return null;
}
android.os.IInterface iin=obj.queryLocalInterface(DESCRIPTOR);if (((iin != null) &&(iin instanceof com.example.aidltest.aidl.forService))) {return((com.example.aidltest.aidl.forService) iin);
}return newcom.example.aidltest.aidl.forService.Stub.Proxy(obj);
}
@Overridepublicandroid.os.IBinder asBinder() {return this;
}
@Overridepublic boolean onTransact(intcode, android.os.Parcel data,
android.os.Parcel reply,intflags)
throws android.os.RemoteException {switch(code) {caseINTERFACE_TRANSACTION: {
reply.writeString(DESCRIPTOR);return true;
}caseTRANSACTION_registerTestCall: {
data.enforceInterface(DESCRIPTOR);
com.example.aidltest.aidl.forActivity _arg0;
_arg0=com.example.aidltest.aidl.forActivity.Stub
.asInterface(data.readStrongBinder());this.registerTestCall(_arg0);
reply.writeNoException();return true;
}caseTRANSACTION_invokCallBack: {
data.enforceInterface(DESCRIPTOR);this.invokCallBack();
reply.writeNoException();return true;
}
}returnsuper.onTransact(code, data, reply, flags);
}private static classProxy implements
com.example.aidltest.aidl.forService {privateandroid.os.IBinder mRemote;
Proxy(android.os.IBinder remote) {
mRemote=remote;
}
@Overridepublicandroid.os.IBinder asBinder() {returnmRemote;
}publicjava.lang.String getInterfaceDescriptor() {returnDESCRIPTOR;
}
@Overridepublic voidregisterTestCall(
com.example.aidltest.aidl.forActivity cb)
throws android.os.RemoteException {
android.os.Parcel _data=android.os.Parcel.obtain();
android.os.Parcel _reply=android.os.Parcel.obtain();try{
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeStrongBinder((((cb!= null)) ?(cb.asBinder())
: (null)));
mRemote.transact(Stub.TRANSACTION_registerTestCall, _data,
_reply,0);
_reply.readException();
}finally{
_reply.recycle();
_data.recycle();
}
}
@Overridepublic voidinvokCallBack() throws android.os.RemoteException {
android.os.Parcel _data=android.os.Parcel.obtain();
android.os.Parcel _reply=android.os.Parcel.obtain();try{
_data.writeInterfaceToken(DESCRIPTOR);
mRemote.transact(Stub.TRANSACTION_invokCallBack, _data,
_reply,0);
_reply.readException();
}finally{
_reply.recycle();
_data.recycle();
}
}
}static final int TRANSACTION_registerTestCall = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);static final int TRANSACTION_invokCallBack = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
}//AIDL中自定义的方法,没有实现
public voidregisterTestCall(com.example.aidltest.aidl.forActivity cb)
throws android.os.RemoteException;public voidinvokCallBack() throws android.os.RemoteException;
}
四、cat MainActivity.javapublic classMainActivity extends ActionBarActivity {
@Overrideprotected voidonCreate(Bundle savedInstanceState) {
......newforService.Stub() {
@Overridepublic voidregisterTestCall(forActivity cb) throws RemoteException {//TODO Auto-generated method stub
}
@Overridepublic voidinvokCallBack() throws RemoteException {//TODO Auto-generated method stub
}
};
......
}
}