Android content providers (2) -- contacts provider

Source: Internet
Author: User

Next, the android content provider-content providers (1)

Contacts provider is a powerful and flexible component in Android. It manages the data in the system address book and provides external access interfaces to access and operate the system address book.

The organization chart of contacts provider is as follows:

It can be seen that the address book of the Android system is a three-tier architecture and accessed through Uri. Next, let's take a look at the class structure and official explanation of each layer:

Contactscontract. ContactsTable

Rows representing different people, based on aggregations of raw contact rows.

Contactscontract. rawcontactsTable

Rows containing a summary of a person's data, specific to a user account and type.

Contactscontract. DataTable

Rows containing the details for raw contact, such as email addresses or phone numbers.

The contacts table contains records of different contacts. The rawcontacts table is a data set of contacts, specifying user accounts and types. The data table stores specific contact information, including emails and phone numbers.

First, let's take a look at row contacts. The following is an official explanation:

A raw contact represents a person's data coming from a single account type and account name. Because the contacts provider allows more than one online service as the source of data for
Person, the contacts provider allows multiple raw contacts for the same person. Multiple raw contacts also allow a user to combine a person's data from more than one account from the same account type.

Generally, row contact stores the account and Account type of the server where user data is located. Because contacts provider allows multiple online services to act as the data source of a contact, users can also use the same service but different accounts to store contact information. It can be understood that row contacts stores the user's account information, including the Account type and account name. Let's take a look at the table structure of row contacts:

Column name




The account name for the Account type that's the source of this raw contact. for example, the account name of a Google account is one of the device owner's Gmail ses SSEs. see the next entryAccount_typeFor
More information.

The format of this name is specific to its account type. It is not necessarily an email address.


The Account type that's the source of this raw contact. For example, the Account type of a Google account isCom. Google. Always qualify your account type with a domain identifier for a domain you own or
Control. This will ensure that your account type is unique.

An Account type that offers contacts data usually has an associated sync adapter that synchronizes with the contacts provider.


The "deleted" flag for a raw contact.

This flag allows the contacts provider to maintain the row internally until sync adapters are able to delete the row from their servers and then finally Delete the row from the repository.

In addition, the official documentation also provides a note: account_name stores the user's account name, such as, account_type is the account type, for example, if it is a Google account, the stored value is com. Google.

Most of the data for a raw contact isn' t stored inContactscontract. rawcontactsTable.
Instead, it's stored in one or more rows inContactscontract. DataTable. Each Data row has a columnData. raw_contact_idThat
ContainsRawcontacts. _ idValue of its parentContactscontract. rawcontactsRow.

Most raw contacts data is not directly stored in rowcontacts tables, but is stored in the data table in the form of one or more rows. Each row has a column of data. raw_contact_id contains a column rawcontacts pointing to the rawcontacts table. _ id, that is, the rawcontacts table stores references. To better understand how row contacts works, this document provides an example:

To understand how raw contacts work, consider the user "Emily Dickinson" who has the following three user accounts defined on her device:

  • Twitter account "belle_of_amherst"

This user has enabledSync contactsFor all three of these accounts inAccountsSettings.

Suppose Emily Dickinson opens a browser window, logs into, Opens
Contacts, and adds "Thomas Higginson". Later on, She logs into GmailEmilyd@gmail.comAnd sends an email to "Thomas Higginson", which automatically adds him as a contact. She
Also follows "colonel_tom" (Thomas Higginson's Twitter ID) on Twitter.

The contacts provider creates three raw contacts as a result of this work:

  1. A raw contact for "Thomas Higginson" The user account
    Type is Google.
  2. A second raw contact for "Thomas Higginson" The user account type is also
    Google. There is a second raw contact even though the name is identical to a previous name, because the person was added for a different user account.
  3. A third raw contact for "Thomas Higginson" associated with "belle_of_amherst". The user account type is Twitter.

In general, a user has three accounts, two of which are Gmail accounts and one is Twitter account. Then, he logs on to one of the Gmail accounts and adds a contact, then he logged on to another Gmail account and sent an email to the previously added contact. Then he automatically added the contact to the currently logged on Gmail account, he followed the contact on Twitter. Then, three pieces of data are added to rawcontacts. The first line is the name of the account that the first Gmail account logs in to. The account type is Google, the second row of data is the name of the account that the second Gmail account logs in to. The account type is still Google, the third row is Twitter, and the account type is Twitter.

Next, let's take a look at how data is stored. The official documents on data provide the following explanations:

Notice that different types of data are stored in this single table. Display name, phone number, email, postal address, photo, and website detail rows are all found inContactscontract. DataTable.
To help manage this,Contactscontract. DataTable has some columns with descriptive names, and others with generic
Names. the contents of a descriptive-name column have the same meaning regardless of the type of data in the row, while the contents of a generic-name column have different meanings depending on the type of data.

Different data types are stored in one table. The names, phone numbers, mailboxes, addresses, and photos are stored in contactscontract. to facilitate data management, data tables have descriptive columns and other general columns. The content of a descriptive column has the same meaning regardless of the data type, but the content of a general column has different meanings based on different data types.

Some examples of descriptive column names are:


The value corresponding to the _ id of rawcontact.


The type of data stored in this row, expressed as a custom MIME type. The contacts provider uses the MIME types defined in the subclassesContactscontract. commondatakinds.
These MIME types are open source, and can be used by any application or sync adapter that works with the contacts provider.

Mimetype defines the data type and specifies the Data Type through commondatakinds.

Generic column names

There are 15 generic columns namedData1ThroughData15That are generally available and an additional four
Generic ColumnsSync1ThroughSync4That shoshould only be used by sync adapters. The generic column name constants always work, regardless of the type of data the row contains.

TheData1Column is indexed. The contacts provider always uses this column for the data that the provider expects will be the most frequent target
Of a query. For example, in an email row, This column contains the actual email address.

By convention, the columnData15Is reserved for storing Binary Large Object (BLOB) data such as photo thumbnails.

There are 15 fields from data1 to data15 to store values. data1 is generally an index column, and contacts provider usually uses this index column for target query. For example, in the mail line, this column stores the actual mail address. In addition, data15 is used to store blob (Binary Large Object) data, such as the user profile picture.

Here is an example to illustrate contactscontract. commondatakinds;

For example,Contactscontract. commondatakinds. EmailClass
Defines type-specific column name constants forContactscontract. DataRow that has the MIME typeEmail. content_item_type.
The class contains the constantAddressFor the email address column. The actual valueAddressIs
"Data1", which is the same as the column's generic name.

The following figure shows a more intuitive understanding:

Commondatakinds. Email stores the field name constants, while the corresponding contactscontract. data stores the actual values. The two use raw_contact_id to correspond to the same rawcontact. Here, data1 stores the address (email address)

The following are some other types of commondatakinds:

Mapping class

Type of data


Contactscontract. commondatakinds. structuredname

The name data for the raw contact associated with this data row.

A raw contact has only one of these rows.

Contactscontract. commondatakinds. Photo

The main photo for the raw contact associated with this data row.

A raw contact has only one of these rows.

Contactscontract. commondatakinds. Email

An email address for the raw contact associated with this data row.

A raw contact can have multiple email addresses.

Contactscontract. commondatakinds. structuredpostal

A postal address for the raw contact associated with this data row.

A raw contact can have multiple postal addresses.

Contactscontract. commondatakinds. groupmembership

An identifier that links the raw contact to one of the groups in the contacts provider.

Groups are an optional feature of an account type and account name. They're described in more detail in the sectioncontact

Not complete...

To join our QQ group or public account, see: Ryan's
Zone public account and QQ Group

Welcome to my Sina Weibo chat: @ Tang Ren _ Ryan

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.