The volume provider stores and obtains data so that the data can be accessed by all applications. They are the only way to share data between applications; there is no public storage area accessible to all Android software packages.
Android is a common data type (audio, video, image, personal contact information, and so on) loaded with a lot of content providers. You can see that some of them are listed in the Android. provider package. You can also query the data contained by these providers (although, for some providers, you must obtain appropriate permissions to read data ).
If you want to expose your data, you have two options: you can create your own content provider (A contentprovider subclass) alternatively, you can add data to the existing providers'-if there is a content providers' control over the same type of data and you have the write permission.
This document is an overview of how to use the content provider. First, a brief basic knowledge discussion, and then explore how to query a content provider, how to modify the data controlled by the content provider, and how to create your own content provider.
Basic knowledge of the Content Provider Basics
How the content provider stores its data on the surface depends on its designers. However, all content providers implement a public interface to query the provider and return results-add, replace, and delete data.
This is an interface directly used by the client, usually through the contentresolver object. You can use getcontentresolver () to obtain a contentresolver from the implementation of an activity or other application component:
Java code:
- ContentResolver cr = getContentResolver ();
Copy code
Then you can use this contentresolver method to interact with any content providers you are interested in.
When initializing a query, the Android system identifies the target content provider and ensures that it is running. The system instantiates all ContentProvider objects. You never need to do it yourself. In fact, you never directly process the ContentProvider object. Generally, there is only one simple instance for each type of ContentProvider. However, it can communicate with multiple ContentProvider objects in different applications and processes. Interaction between processes is handled through the ContentResolver and ContentProvider classes.
Data model The data model
The content provider exposes their data in the form of a simple table on the database model. Each row is a record, and each column is data of special type and meaning. For example, personal information and their phone numbers may be displayed in the following ways:
13 |
(425) 555 6677 |
425 555 6677 |
Kirkland office |
Bully Pulpit |
TYPE_WORK |
44 |
(212) 555-1234 |
212 555 1234 |
NY apartment |
Alan Vain |
TYPE_HOME |
45 |
(212) 555-6657 |
212 555 6657 |
Downtown office |
Alan Vain |
TYPE_MOBILE |
53 |
201.555.4433 |
201 555 4433 |
Love Nest |
Rex Cars |
TYPE_HOME |
Each record contains the _ ID field of a number to uniquely identify the record in this table. IDs can be used to match records in related tables. For example, IDs can be used to search for a person's phone number in a table and photos of the person in another table.
A query returns a Cursor object that can be moved in tables and columns to read the content of each field. It has specific methods to read each data type. Therefore, to read a field, you must understand the data type contained in this field. (The query results and Cursor objects will be discussed later ).
Unique Resource Identifier (URIs)
Each content provider exposes a public URI (wrapped in a Uri object) to uniquely identify its dataset. The content provider that controls multiple datasets (multiple tables) Exposes a separate URI for each dataset. The URIs of all providers starts with the string "content. This content: indicates that the data is being controlled by a content provider.
If you are preparing to define a content provider, you 'd better define a constant for its URI to simplify the client code and make future upgrades clearer. Android defines the content_uri constant for all providers on this platform. For example, the URI of the table matching the personal phone number and the URI of the table containing the personal photo are: (both controlled by the contact contacts content provider)
Android. provider. Contacts. Photos. content_uri
Similarly, the URI of the table and calendar entry for recent phone calls is as follows: Similarly, the Uris for the table of recent phone CILS and the table of calendar entries are:
Android. provider. calllog. CILS. content_uri
Android. provider. Calendar. content_uri
This URI constant is used in all interactions with the content provider. Each contentresolver method uses this URI as its first parameter. It identifies the content provider that contentresolver should talk to and which table of the content provider is its target.
Querying a Content Provider
You need three pieces of information to query a content provider:
URI used to identify the content provider
Name of the data field you want to obtain
Data Types of these fields
If you want to query a record, you also need the ID of the record.
Generate a query Making the query
You can use the ContentResolver. query () method or the Activity. managedQuery () method to query a content provider. Both methods use the same parameter sequence and return a Cursor object. However, managedQuery () enables the activity to manage the lifecycle of the cursor. A managed cursor processes all the details, such as uninstalling itself when the activity is paused, and re-querying itself when the activity is restarted. You can start an Activity to manage a non-managed cursor object by calling Activity. startManagingCursor () as follows ().
Regardless of query () or managedQuery (), their first parameter is the URI-CONTENT_URI constant of the content provider used to identify a particular ContentProvider and Dataset (see URIs earlier ).
To limit the query of only one record, you can extend the _ ID value of the record after the URI-that is, add a string matching the ID at the end of the URI path. For example, if the ID is 23, the URI will be:
There are some helper methods, especially ContentUris. withAppendedId () and Uri. withAppendedPath (), which makes it easy to extend an ID for the URI. Therefore, for example, if you want to search for record 23 in the contact database, you may need to construct the following query statement:
Java code:
- Import android. provider. Contacts. People;
- Import android. content. ContentUris;
- Import android.net. Uri;
- Import android. database. Cursor;
- // Use the ContentUris method to produce the base URI for the contact with _ ID = 23.
- Uri myPerson = ContentUris. withAppendedId (People. CONTENT_URI, 23 );
- // Alternatively, use the Uri method to produce the base URI.
- // It takes a string rather than an integer.
- Uri myPerson = Uri. withAppendedPath (People. CONTENT_URI, "23 ");
- // Then query for this specific record:
- Cursor cur = managedQuery (myPerson, null );
Copy code
Other parameters of the query () and managedQuery () methods limit more query details. As follows:
Name of the data column to be returned. Null returns all columns. Otherwise, only the columns listing names are returned. All content providers on this platform define constants for their columns. For example, the android. provider. Contacts. Phones class defines the constant ID, NUMBER, NUMBER_KEY, NAME, and so on for the names of the columns in the address book described earlier.
Specifies the filter of the returned rows, formatted using an SQL WHERE statement. Null returns all rows. (Unless This URI only queries a single record ).
Select Parameters
Returns the ORDER of rows, formatted in an SQL ORDER BY statement (excluding the ORDER BY statement itself ). The null value indicates that it is returned in the default order of the table, which may be unordered.
Let's look at a query example. This query obtains a list of contact names and preferred phone numbers:
Java code:
- Import android. provider. Contacts. People;
- Import android. database. Cursor;
- // Form an array specifying which columns to return.
- String [] projection = new String [] {
- People. _ ID,
- People. _ COUNT,
- People. NAME,
- People. NUMBER
- };
- // Get the base URI for the People table in the Contacts content provider.
- Uri contacts = People. CONTENT_URI;
- // Make the query.
- Cursor managedCursor = managedQuery (contacts,
- Projection, // Which columns to return
- Null, // Which rows to return (all rows)
- Null, // Selection arguments (none)
- // Put the results in ascending order by name
- People. NAME + "ASC ");
Copy code
This query obtains data from the contact content provider. It gets the name, the preferred phone number, and the unique record ID of each contact. At the same time, it informs the number of returned records in the _ COUNT field of each record.
Constants of column names are defined in different interfaces-_ ID and _ COUNT are defined in BaseColumns, NAME is in PeopleColumns, and NUMBER is in PhoneColumns. The Contacts. People class has implemented these interfaces, which is why the above Code instances can be referenced only by class names.
What a query returns
A query returns a set of zero or more database records. Column names, default order, and their data types are specific to each content provider. However, all providers have a _ ID column that contains the unique ID of each record. In addition, all providers can notify the number of records by returning the _ COUNT column. Its values are the same for all rows.
The following is an example of the returned results of the preceding query:
44 |
3 |
Alan Vain |
212 555 1234 |
13 |
3 |
Bully Pulpit |
425 555 6677 |
53 |
3 |
Rex Cars |
201 555 4433 |
The obtained data is exposed by a Cursor object. You can view the data before and after the result set through the Cursor. You can only use this object to read data. To add, modify, and delete data, you must use a ContentResolver object.