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 />

360<br />

}<br />

};<br />

String link = c.getString(EarthquakeProvider.LINK_COLUMN);<br />

double magnitude = c.getDouble(EarthquakeProvider.MAGNITUDE_COLUMN);<br />

long datems = c.getLong(EarthquakeProvider.DATE_COLUMN);<br />

Date date = new Date(datems);<br />

result.add(new Quake(date, details, location, magnitude, link));<br />

} while(c.moveToNext());<br />

return result;<br />

There are several considerations when implementing these methods:<br />

❑<br />

❑<br />

All exceptions will remain local to the implementing process; they will not be propagated to the<br />

calling application.<br />

All IPC calls are synchronous. If you know that the process is likely to be time-consuming, you<br />

should consider wrapping the synchronous call in an asynchronous wrapper or moving the<br />

processing on the receiver side onto a background thread.<br />

With the functionality implemented, you need to expose this interface to client applications. Expose the<br />

IPC-enabled Service interface by overriding the onBind method within our service implementation to<br />

return an instance of the interface.<br />

The code snippet below demonstrates the onBind implementation for the EarthquakeService:<br />

@Override<br />

public IBinder onBind(Intent intent) {<br />

return myEarthquakeServiceStub;<br />

}<br />

To use the IPC Service from within an Activity, you must bind it as shown in the following code snippet<br />

taken from the Earthquake Activity:<br />

IEarthquakeService earthquakeService = null;<br />

private void bindService() {<br />

bindService(new Intent(IEarthquakeService.class.getName()),<br />

serviceConnection, Context.BIND_AUTO_CREATE);<br />

}<br />

private ServiceConnection serviceConnection = new ServiceConnection() {<br />

public void onServiceConnected(ComponentName className,<br />

IBinder service) {<br />

earthquakeService = IEarthquakeService.Stub.asInterface(service);<br />

}<br />

public void onServiceDisconnected(ComponentName className) {

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

Saved successfully!

Ooh no, something went wrong!