14.01.2013 Views

Android™ Application Development - Bahar Ali Khan

Android™ Application Development - Bahar Ali Khan

Android™ Application Development - Bahar Ali Khan

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 11: Advanced Android <strong>Development</strong><br />

Using AIDL to Support IPC for Services<br />

356<br />

One of the more interesting possibilities of Services is the idea of running independent background<br />

processes to supply processing, data lookup, or other useful functionality to multiple independent<br />

applications.<br />

In Chapter 8, you learned how to create Services for your applications. Here, you’ll learn how to use the<br />

Android Interface Defi nition Language (AIDL) to support interprocess communication (IPC) between<br />

Services and application components. This will give your Services the ability to support multiple applications<br />

across process boundaries.<br />

To pass objects between processes, you need to deconstruct them into OS-level primitives that the<br />

underlying operating system (OS) can then marshal across application boundaries.<br />

AIDL is used to simplify the code that lets your processes exchange objects. It’s similar to interfaces like<br />

COM or Corba in that it lets you create public methods within your Services that can accept and return<br />

object parameters and return values between processes.<br />

Implementing an AIDL Interface<br />

AIDL supports the following data types:<br />

❑ Java language primitives ( int, boolean, float, char, etc.)<br />

❑ String and CharSequence values<br />

❑ List (including generic) objects, where each element is a supported type. The receiving class<br />

will always receive the List object instantiated as an ArrayList.<br />

❑ Map (not including generic) objects in which each key and element is a supported type. The<br />

receiving class will always receive the Map object instantiated as a HashMap.<br />

❑<br />

AIDL-generated interfaces (covered later). An import statement is always needed for these.<br />

❑ Classes that implement the Parcelable interface (covered next). An import statement is always<br />

needed for these.<br />

The following sections demonstrate how to make your application classes AIDL-compatible by implementing<br />

the Parcelable interface, before creating an AIDL interface defi nition and implementing that<br />

interface within your Service.<br />

Passing Custom Class Objects<br />

To pass non-native objects between processes, they must implement the Parcelable interface. This lets<br />

you decompose your objects into primitive types stored within a Parcel that can be marshaled across<br />

process boundaries.<br />

Implement the writeToParcel method to decompose your class object, then implement the public<br />

static Creator fi eld (which implements a new Parcelable.Creator class), which will create a new<br />

object based on an incoming Parcel.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!