Contact class public static class Contacts implements BaseColumns, ContactsColumns, ContactOptionsColumns, ContactNameColumns, ContactStatusColumns
There are 17 data items in the Contacts table:
Variable name column name remarks
_ ID _ id
LOOKUP_KEY lookup
NAME_RAW_CONTACT_ID reference @ 1
The description of DISPLAY_NAME in Contacts is DISPLAY_NAME_PRIMARY.
PHOTO_ID photo_id
IN_VISIBLE_GROUP in_visible_group
HAS_PHONE_NUMBER has_phone_number
TIMES_CONTACTED times_contacted
LAST_TIME_CONTACTED last_time_contacted
STARRED starred
CUSTOM_RINGTONE custom_ringtone
SEND_TO_VOICEMAIL send_to_voicemail
CONTACT_PRESENCE contact_presence
CONTACT_STATUS contact_status
CONTACT_STATUS_TIMESTAMP contact_status_ts
CONTACT_STATUS_RES_PACKAGE contact_status_res_package
CONTACT_STATUS_LABEL contact_status_label
CONTACT_STATUS_ICON contact_status_icon
NOTE 1: In Contacts, the column description includes NAME_RAW_CONTACT_ID, but the corresponding variables and column names are not found.
It is possible that ContentResolver hides this item from the user's point of view. This item should be used only within the system.
NOTE 2: only five items can be written into TIMES_CONTACTED, LAST_TIME_CONTACTED, STARRED, CUSTOM_RINGTONE, SEND_TO_VOICEMAIL.
Operations
Data insertion
A Contact cannot be created explicitly. When a raw contact is inserted, the provider will first try to find a Contact representing the same person.
If one is found, the raw contact's CONTACT_ID column gets the _ ID of the aggregate Contact.
If no match is found, the provider automatically inserts a new Contact
And puts its _ ID into the CONTACT_ID column of the newly inserted raw contact.
The user department can directly insert data. The system automatically inserts data.
When a raw contact is inserted or inserted, the system checks whether the raw contact can be added to an existing group (contact ).
If you find a group that can be added, write the group ID to the CONTACT_ID data item of raw contact.
If not, the system creates a group again and writes the group ID to the CONTACT_ID data item of raw contact.
If the raw contact structured name, organization, phone number, email address, or nickname is changed.
The system checks whether the raw contact is in the Group marked by the CONTACT_ID of the raw contact.
If raw contact does not belong to the group marked by CONTACT_ID of raw contact.
Then the system will find whether the raw contact can be added to other existing groups (contact)
If you find a group that can be added, write the group ID to the CONTACT_ID data item of raw contact.
If not, the system creates a group again and writes the group ID to the CONTACT_ID data item of raw contact.
Note: This group refers to the contact (Aggregation of raw contact)
Data Update
Automatic update: When the RawContacts information contained in a contact changes, the system updates the contact.
Manually force update:
Only certain columns of Contact are modifiable: TIMES_CONTACTED, role, STARRED, CUSTOM_RINGTONE, SEND_TO_VOICEMAIL. Changing any of these columns on the Contact also changes them on all constituent raw contacts.
Only five items can be directly updated by users. They are TIMES_CONTACTED, LAST_TIME_CONTACTED, STARRED, CUSTOM_RINGTONE, SEND_TO_VOICEMAIL.
When they are manually forced to be updated, the corresponding items of RawContacts contained in the contact will also be updated together.
Data deletion
The Contacts document says:
Be careful with deleting Contacts! Deleting an aggregate contact deletes all constituent raw contacts.
The corresponding sync adapters will notice the deletions of their respective raw contacts
And remove them from their back end storage.
Deleting a Contacts deletes all raw contacts contained in it.
However, the following statement cannot delete any Contacts.
GetContentResolver (). delete (Contacts. CONTENT_URI, null,
Null );
However, after deleting all raw Contacts of contacts, Contacts is also deleted.
Query
* If you need to read an individual contact, consider using CONTENT_LOOKUP_URI instead of CONTENT_URI.
* If you need to look up a contact by the phone number, use PhoneLookup. CONTENT_FILTER_URI, which is optimized for this purpose.
* If you need to look up a contact by partial name, e.g. to produce filter-as-you-type suggestions, use the CONTENT_FILTER_URI.
* If you need to look up a contact by some data element like email address, nickname, etc,
Use a query against the ContactsContract. Data table. The result will contain in contact ID, name etc.
The following statement can be used for query:
Cursor c = getContentResolver (). query (Contacts. CONTENT_URI, null,
Null, null, Contacts. DISPLAY_NAME + "collate nocase ");
Note 1: You can use CONTENT_LOOKUP_URI to query a single contact.
NOTE 2: You can use PhoneLookup. CONTENT_FILTER_URI to query phone numbers.
For details, refer to "query telephone numbers with PhoneLookup".
NOTE 3: You can use CONTENT_FILTER_URI to query data by name fuzzy query.
Note 4: You can query the ContactsContract. Data table by email address or nick name.
The query result contains the contact ID, name, and other information. For details, refer to raw contact subtable data query.
Note 5: You can use CONTENT_STREQUENT_URI to query starred and frequently-contacted contacts.
About the Uri of the Contact.
CONTENT_LOOKUP_URI
Public static final Uri CONTENT_LOOKUP_URI = Uri. withAppendedPath (CONTENT_URI, "lookup ");
A content: // style URI for this table that shoshould be used to create shortcuts or otherwise create long-term links to contacts.
This URI shoshould always be followed by a "/" and the contact's LOOKUP_KEY. it can optionally also have a "/" and last known contact ID appended after that. this "complete" format is an important optimization and is highly recommended.
As long as the contact's row ID remains the same, this URI is equivalent to CONTENT_URI.
If the contact's row ID changes as a result of a sync or aggregation, this URI will look up the contact using indirect information (sync IDs or constituent raw contacts ).
Lookup key shoshould be appended unencoded-it is stored in the encoded form, ready for use in a URI.
For example:
Content: // com. android. contacts/lookup/0n/400
Note: "On" is lookUpKey, "400" is Contacts. _ ID
The CONTENT_LOOKUP_URI method is as follows:
1. Obtain the lookUpKey from the database and then synthesize the lookUpUri.
Cursor c = getContentResolver (). query (Contacts. CONTENT_URI, null,
Null, null, Contacts. DISPLAY_NAME + "collate nocase ");
Long contactId = cursor. getLong (cursor. getColumnIndex (Contacts. _ ID ));
// Note that the lookUpKey here is obtained from the Contacts table
String lookUpKey = cursor. getString (cursor. getColumnIndex (Contacts. LOOKUP_KEY ));
Uri lookUpUri = Contacts. getLookupUri (contactId, lookUpKey );
Cursor c = context. getContentResolver (). query (lookUpUri, null,
Null, null, Contacts. DISPLAY_NAME + "collate nocase ");
2. Use contactId to synthesize contactUri, and then use contactUri to synthesize lookUpUri.
Uri contactUri = ContentUris. withAppendedId (Contacts. CONTENT_URI, contactId );
Uri lookUpUri = Contacts. getLookupUri (context. getContentResolver (), contactUri );
In addition, you can obtain contactUri from lookUpUri.
Public static Uri lookupContact (ContentResolver resolver, Uri lookupUri)
Since: API Level 5
Computes a content URI (see CONTENT_URI) given a lookup URI.
Returns null if the contact cannot be found.
The returned content is the CONTENT_URI of a specific contact, for example, content: // com. android. contacts/400.
CONTENT_URI
Public static final Uri CONTENT_URI = Uri. withAppendedPath (AUTHORITY_URI, "contacts ");
All contacts
All contacts are Contacts. CONTENT_URI (content: // com. android. contacts/contacts)
A person contacts
A specific person is in the form of Contacts. CONTENT_URI/contactId.
For example:
Content: // com. android. contacts/400
A personal contacts can be synthesized as follows:
Uri contactUri = ContentUris. withAppendedId (Contacts. CONTENT_URI, contactId );
It can also be used to query a specific person
Cursor c = context. getContentResolver (). query (contentUri, null,
Null, null, Contacts. DISPLAY_NAME + "collate nocase ");
CONTENT_FILTER_URI
Public static final Uri CONTENT_FILTER_URI = Uri. withAppendedPath (CONTENT_URI, "filter ");
The content: // style URI used for "type-to-filter" functionality on the CONTENT_URI URI.
The filter string will be used to match various parts of the contact name.
The filter argument shocould be passed as an additional path segment after this URI.
Fuzzy search for names.
String name = "robin ";
Uri uri = Uri. withAppendedPath (Contacts. CONTENT_FILTER_URI, Uri. encode (name ));
C = context. getContentResolver (). query (uri, null, null );
CONTENT_STREQUENT_URI
Public static final Uri CONTENT_STREQUENT_URI = Uri. withAppendedPath (CONTENT_URI, "strequent ");
The content: // style URI for this table joined with useful data from ContactsContract. Data,
Filtered to include only starred contacts and the most frequently contacted contacts.
It is also used to query contacts, but only returns starred and frequently-contacted contact information.
For example:
C = context. getContentResolver (). query (Contacts. CONTENT_STREQUENT_URI, null );
The returned column is 17 items of data, just like the CONTENT_URI query.
CONTENT_STREQUENT_FILTER_URI
Public static final Uri CONTENT_STREQUENT_FILTER_URI = Uri. withAppendedPath (CONTENT_STREQUENT_URI, "filter ");
The content: // style URI used for "type-to-filter" functionality on the CONTENT_STREQUENT_URI URI.
The filter string will be used to match various parts of the contact name.
The filter argument shocould be passed as an additional path segment after this URI.
It is used to fuzzy query contacts by name, but only returns starred and frequently-contacted contact information.
For example:
String name = "robin ";
Uri uri = Uri. withAppendedPath (Contacts. CONTENT_STREQUENT_FILTER_URI, Uri. encode (name ));
C = context. getContentResolver (). query (uri, null, null );
The returned column is 17 items of data, just like the CONTENT_URI query.