分类: LINUX
2009-06-15 13:10:33
1.4.2 細說Stub類別的程式碼
這個Stub類別就是由aidl.exe所產出的;也就是在calInterface.java裡面。茲再重複列出calInterface.java程式碼如下:
/*---- calInterface.java ----*/
/*
* This file is auto-generated. DO NOT MODIFY.
* Original file: calInterface.aidl
*/
package com.misoo.gx06;
import java.lang.String;
import android.os.RemoteException;
import android.os.IBinder;
import android.os.IInterface;
import android.os.Binder;
import android.os.Parcel;
public interface calInterface extends android.os.IInterface
{
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements com.misoo.gx06.calInterface
{
private static final java.lang.String DESCRIPTOR = "com.misoo.gx06.calInterface";
/** Construct the stub at attach it to the interface. */
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/**
* Cast an IBinder object into an calInterface interface,
* generating a proxy if needed.
*/
public static com.misoo.gx06.calInterface asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
com.misoo.gx06.calInterface in = (com.misoo.gx06.calInterface)obj.queryLocalInterface(DESCRIPTOR);
if ((in!=null)) {
return in;
}
return new com.misoo.gx06.calInterface.Stub.Proxy(obj);
}
public android.os.IBinder asBinder()
{
return this;
}
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_EvDigitPress:
{
data.enforceInterface(DESCRIPTOR);
int _arg0;
_arg0 = data.readInt();
java.lang.String _result = this.EvDigitPress(_arg0);
reply.writeNoException();
reply.writeString(_result);
return true;
}
case TRANSACTION_EvCPress:
{
data.enforceInterface(DESCRIPTOR);
java.lang.String _result = this.EvCPress();
reply.writeNoException();
reply.writeString(_result);
return true;
}
case TRANSACTION_EvPlusPress:
{
data.enforceInterface(DESCRIPTOR);
java.lang.String _result = this.EvPlusPress();
reply.writeNoException();
reply.writeString(_result);
return true;
}
case TRANSACTION_EvAssignPress:
{
data.enforceInterface(DESCRIPTOR);
java.lang.String _result = this.EvAssignPress();
reply.writeNoException();
reply.writeString(_result);
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
private static class Proxy implements com.misoo.gx06.calInterface
{
private android.os.IBinder mRemote;
Proxy(android.os.IBinder remote)
{
mRemote = remote;
}
public android.os.IBinder asBinder()
{
return mRemote;
}
public java.lang.String getInterfaceDescriptor()
{
return DESCRIPTOR;
}
public java.lang.String EvDigitPress(int d) throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
java.lang.String _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeInt(d);
mRemote.transact(Stub.TRANSACTION_EvDigitPress, _data, _reply, 0);
_reply.readException();
_result = _reply.readString();
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
public java.lang.String EvCPress() throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
java.lang.String _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
mRemote.transact(Stub.TRANSACTION_EvCPress, _data, _reply, 0);
_reply.readException();
_result = _reply.readString();
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
public java.lang.String EvPlusPress() throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
java.lang.String _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
mRemote.transact(Stub.TRANSACTION_EvPlusPress, _data, _reply, 0);
_reply.readException();
_result = _reply.readString();
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
public java.lang.String EvAssignPress() throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
java.lang.String _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
mRemote.transact(Stub.TRANSACTION_EvAssignPress, _data, _reply, 0);
_reply.readException();
_result = _reply.readString();
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
}
static final int TRANSACTION_EvDigitPress = (IBinder.FIRST_CALL_TRANSACTION + 0);
static final int TRANSACTION_EvCPress = (IBinder.FIRST_CALL_TRANSACTION + 1);
static final int TRANSACTION_EvPlusPress = (IBinder.FIRST_CALL_TRANSACTION + 2);
static final int TRANSACTION_EvAssignPress = (IBinder.FIRST_CALL_TRANSACTION + 3);
}
public java.lang.String EvDigitPress(int d) throws android.os.RemoteException;
public java.lang.String EvCPress() throws android.os.RemoteException;
public java.lang.String EvPlusPress() throws android.os.RemoteException;
public java.lang.String EvAssignPress() throws android.os.RemoteException;
}
這個calInterface.java檔案是由aidl.exe工具程式所產出的。
首先,calInterface介面繼承了Android提供的IInterface介面,如下述程式碼:
public interface calInterface extends android.os.IInterface
{
………
}
這個類別定義又內含有一個Stub類別,其繼承Android提供的Binder父類別,而且實作calInterface介面,如下述程式碼:
public interface calInterface extends android.os.IInterface
{
……….
public static abstract class Stub extends android.os.Binder implements com.misoo.gx06.calInterface
{
……….
private static class Proxy implements com.misoo.gx06.calInterface
{
………
}
………..
}
由於Stub類別繼承了Binder父類別,而Binder又實作了IBinder介面,所以Stub類別支持兩個介面:IBinder和calInterface。其內含的Proxy類別也支持calInterface。
圖1-7 IBinder與calInterface介面之關係
1.4.4 細說CalBinder類別的程式碼
基於這個結構,就可以從Stub類別衍生(Derive)出子類別:calBinder,其程式碼為:
/*---- CalBinder.java ----*/
package com.misoo.gx06;
import android.os.RemoteException;
public class CalBinder extends calInterface.Stub{
private Calculator mCal = null;
public CalBinder(){
mCal = new Calculator();
}
@Override
public String EvAssignPress() throws RemoteException {
mCal.EvAssignPress();
return mCal.retStr;
}
@Override
public String EvCPress() throws RemoteException {
mCal.EvCPress();
return mCal.retStr;
}
@Override
public String EvDigitPress(int d) throws RemoteException {
mCal.EvDigitPress(d);
return mCal.retStr;
}
@Override
public String EvPlusPress() throws RemoteException {
mCal.EvPlusPress();
return mCal.retStr;
}
}
其中,指令:
public CalBinder(){
mCal = new Calculator();
}
誕生了一個Calculator物件,如此與Calculator類別銜接起來了。
1.4.5 函數呼叫的情境
此範例程式需要用到各AIDL類別的詳細函數如下圖所示:
圖1-8 AIDL各類別裡的函數及其關係
只要對物件觀念和技術較為熟悉的話,就可看出來其並不太複雜,只是基本物件觀念之應用而已。更詳細之說明,請看高煥堂所寫的第2本Android書:<