Exploring Android’s Content Providers Android

Android devices ship with a number of built-in applications, many of which expose their data as content providers. Your application can access content provider data from a variety of sources. You can find the content providers included with Android in the package android.provider. Table lists some useful content providers in this package.

Useful Built-In Content Providers

Useful Built-In Content Providers

Now let’s look at the individual content providers in more detail.

Using the MediaStore Content Provider

You can use the MediaStore content provider to access media on the phone and on external storage devices. The primary types of media that you can access are audio, images, and video. You can access these different types of media through their respective content provider classes under android.provider.MediaStore.

Most of the MediaStore classes allow full interaction with the data. You can retrieve, add, and delete media files from the device. There are also a handful of helper classes that define the most common data columns that can be requested.

Table lists some commonly used classes that you can find under android. provider. MediaStore.

Common MediaStore Classes

Common MediaStore Classes

The following code demonstrates how to request data from a content provider. A query is made to the MediaStore to retrieve the titles of all the audio files on the SD card of the handset and their respective durations. This code requires that you load some audio files onto the virtual SD card in the emulator.

The MediaStore.Audio.Media class has predefined strings for every data field (or column) exposed by the content provider. You can limit the audio file data fields requested as part of the query by defining a string array with the column names required. In this case, we limit the results to only the track title and the duration of each audio file.

We then use a managedQuery() method call. The first parameter is the predefined URI of the content provider you want to query (in most cases, the primary external storage is the SD card).The second parameter is the list of columns to return (audio file titles and durations).The third and fourth parameters control any selection filtering arguments, and the fifth parameter provides a sort method for the results. We leave these null, as we want all audio files at this location. By using the managedQuery() method, we get a managed Cursor as a result. We then examine our Cursor for the results.

Using the CallLog Content Provider

Android provides a content provider to access the call log on the handset via the class android.provider.CallLog. At first glance, the CallLog might not seem to be a useful provider for developers, but it has some nifty features. You can use the CallLog to filter recently dialed calls, received, and missed calls. The date and duration of each call is logged and tied back to the Contact application for caller identification purposes.

The CallLog is a useful content provider for customer relationship management (CRM) applications. The user can also tag specific phone numbers with custom labels within the Contact application.

To demonstrate how the CallLog content provider works, let’s look at a hypothetical situation where we want to generate a report of all calls to a number with the custom labeled HourlyClient123.Android allows for custom labels on these numbers, which we leverage for this example:

This code is similar to the code shown for the MediaStore audio files.Again, we start with listing our requested columns: the call label and the duration of the call. This time, however, we don’t want to get every call in the log, only those with a label of HourlyClient123.To filter the results of the query to this specific label, it is necessary to specify the third and fourth parameters of the managedQuery() call. Together, these two parameters are equivalent to a database WHERE clause. The third parameter specifies the format of the WHERE clause with the column name with selection parameters (shown as ?s) for each selection argument value. The fourth parameter, the String array, provides the values to substitute for each of the selection arguments (?s) in order as you would do for a simple SQLite database query.

As before, the Activity manages the Cursor object lifecycle. We use the same method to iterate the records of the Cursor and add up all the call durations.

Accessing Content Providers That Require Permissions

Your application needs a special permission to access the information provided by the CallLog content provider. You can declare the uses-permission tag using the Eclipse Wizard or by adding the following to your AndroidManifest.xml file:

Although it’s a tad confusing, there is no CallLog permission. Instead, applications that access the CallLog use the READ_CONTACTS permission. Although the values are cached within this content provider, the data is similar to what you might find in the contacts provider.

Using the Browser Content Provider

Another useful, built-in content provider is the Browser. The Browser content provider exposes the user’s browser site history and their bookmarked websites. You access this content provider via the android.provider.Browser class. As with the CallLog class, you can use the information provided by the Browser content provider to generate statistics and to provide cross-application functionality. You might use the Browser content provider to add a bookmark for your application support website.

In this example, we query the Browser content provider to find the top five most frequently visited bookmarked sites.

Again,the requested columns are defined, the query is made, and the cursor iterates through the results.

Note that the managedQuery() call has become substantially more complex. Let’s take a look at the parameters to this method in more detail. The first parameter, Browser .BOOKMARKS_URI, is a URI for all browser history, not only the Bookmarked items. The second parameter defines the requested columns for the query results. The third parameter specifies that the bookmark property must be true. This parameter is needed in order to filter within the query. Now the results are only browser history entries that have been bookmarked. The fourth parameter, selection arguments, is used only when replacement values are used, which is not used in this case, so the value is set to null. Lastly, the fifth parameter specifies an order to the results (most visited in descending order). Retrieving browser history information requires setting the READ_ HISTORY_ BOOKMARKS permission.

Using the Contacts Content Provider

The Contacts database is one of the most commonly used applications on the mobile phone. People always want phone numbers handy for calling friends, family, coworkers, and clients.Additionally, most phones show the identity of the caller based on the contacts application, including nicknames, photos, or icons.

Android provides a built-in Contact application, and the contact data is exposed to other Android applications using the content provider interface. As an application developer, this means you can leverage the user’s contact data within your application for a more robust user experience.

Accessing Private Contact Data

Your application needs special permission to access the private user information provided by the Contacts content provider. You must declare a uses-permission tag using the permission READ_CONTACTS to read this information. The code to start reading contact data from the Contacts application should look familiar.

This short example simply shows querying for a single contact. We used LIMIT to retrieve one contact record. If you actually look at the returned columns of data, you find that there is little more than the contact name and some indexes. The data fields are not explicitly returned. Instead, the results include the values needed to build specific URIs to those pieces of data. We need to request the data for the contact using these indexes.

Specifically,we retrieve the primary email and primary phone number for this contact.

Now that we have the column index values for the contact’s name, primary email address, and primary phone number, we need to build the Uri objects associated with those pieces of information and query for the primary email and primary phone number.

After retrieving the appropriate column indexes for a contact’s specific email and phone number, we call ContentUris.withAppendedId() to create the new Uri objects from existing ones and the identifiers we now have. This enables direct selection of a particular row from the table when the index of that row is known. You can use a selection parameter to do this, as well. Lastly, we used the two new Uri objects to perform two calls to managed Query().

Now we take a shortcut with the requested columns String array because each query only has one column:

If an email or phone number doesn’t exist, an exception called android. database.Cursor IndexOut OfBounds Exception is thrown. This can be caught, or you can check to see that a result was actually returned in the Cursor first.

Querying for a Specific Contact

If that seemed like quite a lot of coding to get a phone number, you’re not alone. For getting a quick piece of data, there is a faster way. The following block of code demonstrates how we can get the primary number and name for one contact. The primary number for a contact is designated as the default number within the contact manager on the handset. It might be useful to use the primary number field if you don’t get any results back from the query.

This block of code should look somewhat familiar, yet it is a much shorter and more straightforward method to query for phone numbers by Contact name. The Contacts. Phones. CONTENT _URI contains phone numbers but it also happens to have the contact name. This is similar to the CallLog content provider.

Using the UserDictionary Content Provider

Another useful content provider is the UserDictionary provider. You can use this content provider for predictive text input on text fields and other user input mechanisms. Individual words stored in the dictionary are weighted by frequency and organized by locale. You can use the addWord() method within the UserDictionary. Words class to add words to the custom user dictionary.

Using the Settings Content Provider

Another useful content provider is the Settings provider. You can use this content provider to access the device settings and user preferences. Settings are organized much as they are in the Settings application—by category. You can find information about the Settings content provider in the android.provider.Settings class.

Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Android Topics