Defining a Service Interface in AIDL

To demonstrate an example of a remote service, we are going to write a stock-quoter service. This service will provide a method that takes a ticker symbol and returns the stock value. To write a remote service in Android, the first step is to define the service definition in an AIDL file. Listing 8-10 shows the AIDL definition of IStockOuoteService.

Listing 8-10. The AIDL Definition of the Stock-Quoter Service package com.syh;

interface IStockOuoteService {

double getOuote(String ticker);

The IStockOuoteService accepts the stock-ticker symbol as a string and returns the current stock value as a double. When you create the AIDL file, the Android Eclipse plug-in runs the AIDL compiler to process your AIDL file (as part of the build process). If your AIDL file compiles successfully, the compiler generates a Java interface suitable for RPC communication. Note that the generated file will be in the package named in your AIDL file—com.syh, in this case.

Listing 8-11 shows the generated Java file for our IStockOuoteService. Note that if you are using the Android 1.5 SDK, the generated file will be within the gen folder. See Chapter 12 for details.

Listing 8-11. The Compiler-Generated Java File package com.syh;

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 IStockOuoteService extends android.os.IInterface {

/** Local-side IPC implementation stub class. */

public static abstract class Stub extends android.os.Binder implements com.syh.IStockOuoteService {

private static final java.lang.String DESCRIPTOR = "com.syh.IStockOuoteService"; /** Construct the stub at attach it to the interface. */

this.attachInterface(this, DESCRIPTOR); }

* Cast an IBinder object into an IStockOuoteService interface,

* generating a proxy if needed.

public static com.syh.IStockOuoteService asInterface(android.os.IBinder obj) {

return null; }

com.syh.IStockOuoteService in = (com.syh.IStockOuoteService)


return new com.syh.IStockOuoteService.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



return true; }

case TRANSACTION_getOuote: {

data.enforceInterface(DESCRIPTOR); java.lang.String _arg0;

double _result = this.get0uote(_arg0);



return true; }

return super.onTransact(code, data, reply, flags); }

private static class Proxy implements com.syh.IStockOuoteService {

private android.os.IBinder mRemote;

Proxy(android.os.IBinder remote) {

public android.os.IBinder asBinder() {

return mRemote; }

public java.lang.String getInterfaceDescriptor() {

return DESCRIPTOR; }

public double get0uote(java.lang.String ticker) throws android.os.RemoteException {

android.os.Parcel _data = android.os.Parcel.obtain(); android.os.Parcel _reply = android.os.Parcel.obtain(); double _result; try {

_data.writeInterfaceToken(DESCRIPTOR); _data.writeString(ticker);

mRemote. transact(Stub.TRANSACTION_get0uote, _data, _reply, 0); _reply.readException();

finally { _reply.recycle();

return _result; }

static final int TRANSACTION_get0uote = (IBinder.FIRST_CALL_TRANSACTION + 0); }

public double get0uote(java.lang.String ticker) throws android.os.RemoteException; }

Note the following important points regarding the generated classes:

• The interface we defined in the AIDL file is implemented as an interface in the generated code (that is, there is an interface named IStockOuoteService).

• A static final abstract class named Stub extends android.os.Binder and implements IStockOuoteService. Note that the class is an abstract class.

• An inner class named Proxy implements the IStockOuoteService that proxies the Stub class.

• The AIDL file must reside in the package where the generated files are supposed to be (as specified in the AIDL file's package declaration).

Now let's move on and implement the AIDL interface in a service class.

0 0

Post a comment