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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

Methods can take zero or more parameters and return void or a supported type. If you defi ne a method<br />

that takes one or more parameters, you need to use a directional tag to indicate if the parameter is a<br />

value or reference type using the in, out, and inout keywords.<br />

Where possible, you should limit the direction of each parameter, as marshaling parameters is an expensive<br />

operation.<br />

The following sample shows a basic AIDL defi nition for the IEarthquakeService.aidl fi le:<br />

package com.paad.earthquake;<br />

import com.paad.earthquake.Quake;<br />

interface IEarthquakeService {<br />

List getEarthquakes();<br />

}<br />

void refreshEarthquakes();<br />

Implementing and Exposing the IPC Interface<br />

If you’re using the ADT plug-in, saving the AIDL fi le will automatically code-generate a Java interface<br />

fi le. This interface will include an inner Stub class that implements the interface as an abstract class.<br />

Have your Service extend the Stub and implement the functionality required. Typically, this will be<br />

done using a private fi eld variable within the Service whose functionality you’ll be exposing.<br />

The following code snippet shows an implementation of the IEarthquakeService AIDL defi nition<br />

created above:<br />

IBinder myEarthquakeServiceStub = new IEarthquakeService.Stub() {<br />

public void refreshEarthquakes() throws RemoteException {<br />

EarthquakeService.this.refreshEarthquakes();<br />

}<br />

public List getEarthquakes() throws RemoteException {<br />

ArrayList result = new ArrayList();<br />

ContentResolver cr = EarthquakeService.this.getContentResolver();<br />

Cursor c = cr.query(EarthquakeProvider.CONTENT_URI, null, null, null, null);<br />

if (c.moveToFirst())<br />

do {<br />

Double lat = c.getDouble(EarthquakeProvider.LATITUDE_COLUMN);<br />

Double lng = c.getDouble(EarthquakeProvider.LONGITUDE_COLUMN);<br />

Location location = new Location(“dummy”);<br />

location.setLatitude(lat);<br />

location.setLongitude(lng);<br />

String details = c.getString(EarthquakeProvider.DETAILS_COLUMN);<br />

359

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

Saved successfully!

Ooh no, something went wrong!