aidl生成java文件_Android AIDL自动生成Java文件测试

/********************************************************************************

* 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

}

};

......

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值