Contact類 public static class Contacts implements BaseColumns, ContactsColumns, ContactOptionsColumns, ContactNameColumns, ContactStatusColumns
對Contacts表共17項資料:
變數名 列名 備忘
_ID _id
LOOKUP_KEY lookup
NAME_RAW_CONTACT_ID參照@1
DISPLAY_NAME display_name 在Contacts對列的描述為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
注意1:在Contacts對列的描述有NAME_RAW_CONTACT_ID,但沒找到相應的變數和列名。
可能ContentResolver覺得把該項暴露給使用者每什麼意義,就把它隱藏起來了。該項應該是只給系統內部用的哦。
注意2:只有五項是可寫TIMES_CONTACTED, LAST_TIME_CONTACTED, STARRED, CUSTOM_RINGTONE, SEND_TO_VOICEMAIL.
Operations
資料插入
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.
使用者部能直接插入資料。資料的插入是由系統自動來完成的。
當raw contact被插入或時,系統就會檢查是否可以把該raw contact加到已經有的組(contact)。
如果找到可以加入的組,就把組ID寫到raw contact的CONTACT_ID這個資料項目中。
如果沒找到就由系統重新建立一組,並把組ID寫到raw contact的CONTACT_ID這個資料項目中。
如果raw contact的structured name, organization, phone number, email address, or nickname被改變。
系統就會檢查raw contact是否還屬於raw contact的CONTACT_ID標記的那個組。
如果發現raw contact不屬於raw contact的CONTACT_ID標記的那個組。
那麼系統就會找是否可以把該raw contact加到已經有的其他組(contact)
如果找到可以加入的組,就把組ID寫到raw contact的CONTACT_ID這個資料項目中。
如果沒找到就由系統重新建立一組,並把組ID寫到raw contact的CONTACT_ID這個資料項目中。
注意:這的組是指contact(Aggregation of raw contact)
資料更新
自動更新:當contact所包含的RawContacts的資訊發生改變時,系統會對contact做相應的更新。
手動強制更新:
Only certain columns of Contact are modifiable: TIMES_CONTACTED, LAST_TIME_CONTACTED, STARRED, CUSTOM_RINGTONE, SEND_TO_VOICEMAIL. Changing any of these columns on the Contact also changes them on all constituent raw contacts.
只有五項是可被使用者直接更新。它們是TIMES_CONTACTED, LAST_TIME_CONTACTED, STARRED, CUSTOM_RINGTONE, SEND_TO_VOICEMAIL。
當它們被手動強制更新時,contact所包含的RawContacts的相應項也會被一起更新。
資料刪除
Contacts文檔說:
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.
刪除一個Contacts,會把它所包含所有raw contacts都刪除掉。
但是用下面語句居然刪除不了任何Contacts.
getContentResolver().delete(Contacts.CONTENT_URI, null,
null);
不過在刪除Contacts的所有raw contacts後,Contacts也被刪除了。
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 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 contact ID, name etc.
可用如下語句進行查詢:
Cursor c=getContentResolver().query(Contacts.CONTENT_URI, null,
null, null, Contacts.DISPLAY_NAME + " COLLATE NOCASE");
注意1:對於查單個contact資訊時,可用CONTENT_LOOKUP_URI來進行查詢
注意2:對於想通過電話號碼查詢時,可用PhoneLookup.CONTENT_FILTER_URI來查詢。
具體參照《用PhoneLookup進行電話號碼查詢》
注意3:對於想通過名字的模糊查詢來查詢時,可以CONTENT_FILTER_URI URI來查詢。
注意4: 對於想通過email address, nickname來查詢時,可查詢ContactsContract.Data表。
它的查詢結果裡麵包含了contact ID, name等資訊。具體可參考《raw contact子表資料查詢》
注意5:可以使用CONTENT_STREQUENT_URI來查詢starred和經常聯絡的連絡人資訊
關於Contact的Uri介紹.
CONTENT_LOOKUP_URI
public static final Uri CONTENT_LOOKUP_URI = Uri.withAppendedPath(CONTENT_URI, "lookup");
A content:// style URI for this table that should be used to create shortcuts or otherwise create long-term links to contacts.
This URI should 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 should be appended unencoded - it is stored in the encoded form, ready for use in a URI.
例如下:
content://com.android.contacts/contacts/lookup/0n/400
注意:"On"是lookUpKey,"400"是Contacts._ID
得到CONTENT_LOOKUP_URI的方式有:
1,從資料庫中取得lookUpKey,然後再合成lookUpUri
Cursor c=getContentResolver().query(Contacts.CONTENT_URI, null,
null, null, Contacts.DISPLAY_NAME + " COLLATE NOCASE");
Long contactId=cursor.getLong(cursor.getColumnIndex(Contacts._ID));
//注意這裡的lookUpKey是從Contacts表中取的
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,先用contactId合成contactUri,再用contactUri然後再合成lookUpUri.
Uri contactUri=ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
Uri lookUpUri=Contacts.getLookupUri(context.getContentResolver(), contactUri);
另外可以從lookUpUri得到contactUri。
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.
這裡返回的是具體一個連絡人的CONTENT_URI比如:content://com.android.contacts/contacts/400
CONTENT_URI
public static final Uri CONTENT_URI = Uri.withAppendedPath(AUTHORITY_URI, "contacts");
全部contacts
全部contacts就是Contacts.CONTENT_URI(content://com.android.contacts/contacts)
某個人contacts
具體某個人就是Contacts.CONTENT_URI/contactId的形式。
比如:
content://com.android.contacts/contacts/400
某個人contacts可用如下方式合成
Uri contactUri=ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
也可用它對具體一個人進行查詢
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 should be passed as an additional path segment after this URI.
用於對名字進行模糊查詢。
String name="robin";
Uri uri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI, Uri.encode(name));
c=context.getContentResolver().query(uri, null,null,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.
它也是用於對連絡人進行查詢,但是只返回starred和經常聯絡的連絡人資訊。
比如:
c=context.getContentResolver().query(Contacts.CONTENT_STREQUENT_URI, null,null,null,null);
返回的列和CONTENT_URI查詢一樣,也是17項資料。
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 should be passed as an additional path segment after this URI.
它是用於通過名字來對連絡人進行模糊查詢,但是只返回starred和經常聯絡的連絡人資訊。
比如:
String name="robin";
Uri uri = Uri.withAppendedPath(Contacts.CONTENT_STREQUENT_FILTER_URI, Uri.encode(name));
c=context.getContentResolver().query(uri, null,null,null,null);
返回的列和CONTENT_URI查詢一樣,也是17項資料。