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.

}<br />

}<br />

Chapter 6: Data Storage, Retrieval, and Sharing<br />

default: throw new IllegalArgumentException(“Unsupported URI:” +<br />

uri);<br />

The fi nal step in creating a Content Provider is defi ning the MIME type that identifi es the data the provider<br />

returns.<br />

Override the getType method to return a String that uniquely describes your data type. The type<br />

returned should include two forms, one for a single entry and another for all the entries, following the<br />

forms:<br />

❑<br />

❑<br />

Single Item<br />

vnd..cursor.item/<br />

All Items<br />

vnd..cursor.dir/<br />

The following code snippet shows how to override the getType method to return the correct MIME<br />

type based on the URI passed in:<br />

@Override<br />

public String getType(Uri _uri) {<br />

switch (uriMatcher.match(_uri)) {<br />

case ALLROWS: return “vnd.paad.cursor.dir/myprovidercontent”;<br />

case SINGLE_ROW: return “vnd.paad.cursor.item/myprovidercontent”;<br />

default: throw new IllegalArgumentException(“Unsupported URI: “ +<br />

_uri);<br />

}<br />

}<br />

Registering Your Provider<br />

Once you have completed your Content Provider, it must be added to the application manifest.<br />

Use the authorities tag to specify its address, as shown in the following XML snippet:<br />

<br />

Creating and Using an Earthquake Content Provider<br />

Having created an application that features a list of earthquakes, you have an excellent opportunity to<br />

share this information with other applications.<br />

By exposing these data through a Content Provider, you, and others, can create new applications based<br />

on earthquake data without having to duplicate network traffi c and the associated XML parsing.<br />

197

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

Saved successfully!

Ooh no, something went wrong!