Login     Sign Up
Tugadar (@admin)
17 days ago
15 Views

Content Providers are a generic interface mechanism that lets you share data between applications. By abstracting away the underlying data source, Content Providers let you decouple your application layer from the data layer, making your applications data-source agnostic.

Content Providers feature full permission control and are accessed using a simple URI model. Shared content can be queried for results as well as supporting write access. As a result, any application with the appropriate permissions can add, remove, and update data from any other applications — including some native Android databases.

Many of the native databases have been made available as Content Providers, accessible by third-party applications. This means that your applications can have access to the phone’s Contact Manager, media player, and other native database once they’ve been granted permission.

By publishing your own data sources as Content Providers, you make it possible for you (and other developers) to incorporate and extend your data in new applications.

Using Content Providers

Access to Content Providers is handled by the ContentResolver class.

The following sections demonstrate how to access a Content Resolver and how to use it to query and transact with a Content Provider. They also demonstrate some practical examples using the native Android Content Providers.

Introducing Content Resolvers

Each application Context has a single ContentResolver, accessible using the getContentResolver method, as shown in the following code snippet:

ContentResolver cr = getContentResolver();

Content Resolver includes several methods to transact and query Content Providers. You specify the provider to interact using a URI.

A Content Provider’s URI is defined by its authority as defined in its application manifest node. An authority URI is an arbitrary string, so most providers expose a CONTENT_URI property that includes its authority.

Content Providers usually expose two forms of URI, one for requests against all the data and another that specifies only a single row. The form for the latter appends /<rowID> to the standard CONTENT_URI.

Querying for Content

As in databases, query results are returned as Cursors over a result set. You can extract values from the cursor using the techniques described previously within the database section on

“Extracting Results from a Cursor.”

Content Provider queries take a very similar form to database queries. Using the query method on the

ContentResolver object, pass in:

❑The URI of the content provider data you want to query

❑A projection that represents the columns you want to include in the result set

❑A where clause that defines the rows to be returned. You can include ? wild cards that will be replaced by the values stored in the selection argument parameter.

❑ An array of selection argument strings that will replace the ?’s in the where clause

❑ A string that describes the order of the returned rows

The following skeleton code demonstrates the use of a Content Resolver to apply a query to a Content Provider:

// Return all rows

Cursor allRows = getContentResolver().query(MyProvider.CONTENT_URI,

null, null, null, null);

Return all columns for rows where column 3 equals a set value

and the rows are ordered by column 5.

String where = KEY_COL3 + “=” + requiredValue; String order = KEY_COL5;

Cursor someRows = getContentResolver().query(MyProvider.CONTENT_URI,

null, where, null, order);

You’ll see some more practical examples of querying for content later in this chapter when the native Android content providers are introduced.