ContactsContract 类中有很多内嵌的类帮助操作Contect Provider中的Contracts,其中用的比较多的有Data类对应数据库的Data表(比较重要)、RawContrats类对应的raw_contacts表、Groups类对应的groups表。Data表非常重要,用于存储各种类型的数据,用MIMEType类进行区分,数据存储在Data1~Data15中,各种类型不一样,需要查看相应的MIMEType,例如有如下一些类型(来源于http://developer.android.com/reference/android/provider/ContactsContract.Data.html):
The MIME type of the item represented by this row. Examples of common MIME types are:
StructuredName.CONTENT_ITEM_TYPE
Phone.CONTENT_ITEM_TYPE
Email.CONTENT_ITEM_TYPE
Photo.CONTENT_ITEM_TYPE
Organization.CONTENT_ITEM_TYPE
Im.CONTENT_ITEM_TYPE
Nickname.CONTENT_ITEM_TYPE
Note.CONTENT_ITEM_TYPE
StructuredPostal.CONTENT_ITEM_TYPE
GroupMembership.CONTENT_ITEM_TYPE
Website.CONTENT_ITEM_TYPE
Event.CONTENT_ITEM_TYPE
Relation.CONTENT_ITEM_TYPE
SipAddress.CONTENT_ITEM_TYPE
Data表
同理:rawContacts、Groups表也有相应的对应情况,值得注意的是这几个表相互联系,例如Data表中的通过raw_contact_id与raw_contacts表中的_id相联系,构成多对一关系,group表中的 _id与Data表中的Data1对应(前提是MIMETYPE= ContactsContract.CommonDataKinds.GroupMemeberShip),因此推理,Data表可以与其他的表进一步向关联,所以Data表应该是所有表的中介。当然这些表之间也有冗余,猜测其目的是为了方便于查找的,因此如果要对表进行更改,需要注意有没有相联系的表需要进行相应的更改操作(好像这里有隐含同步的操作方式,只要操作一个表,同步机制会相应的更改联系的表,待以后研究)
参考文档中还推荐了几种操作的代码,可供参考:
-
Insert
-
An individual data row can be inserted using the traditional
insert(Uri, ContentValues)
method. Multiple rows should always be inserted as a batch.An example of a traditional insert:
ContentValues values = new ContentValues(); values.put(Data.RAW_CONTACT_ID, rawContactId); values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE); values.put(Phone.NUMBER, "1-800-GOOG-411"); values.put(Phone.TYPE, Phone.TYPE_CUSTOM); values.put(Phone.LABEL, "free directory assistance"); Uri dataUri = getContentResolver().insert(Data.CONTENT_URI, values);
The same done using ContentProviderOperations:
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI) .withValue(Data.RAW_CONTACT_ID, rawContactId) .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE) .withValue(Phone.NUMBER, "1-800-GOOG-411") .withValue(Phone.TYPE, Phone.TYPE_CUSTOM) .withValue(Phone.LABEL, "free directory assistance") .build()); getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
Update
-
Just as with insert, update can be done incrementally or as a batch, the batch mode being the preferred method:
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI) .withSelection(Data._ID + "=?", new String[]{String.valueOf(dataId)}) .withValue(Email.DATA, "somebody@android.com") .build()); getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
Delete
-
Just as with insert and update, deletion can be done either using the
delete(Uri, String, String[])
method or using a ContentProviderOperation:ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); ops.add(ContentProviderOperation.newDelete(Data.CONTENT_URI) .withSelection(Data._ID + "=?", new String[]{String.valueOf(dataId)}) .build()); getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
Query
-
-
Finding all Data of a given type for a given contact
-
Cursor c = getContentResolver().query(Data.CONTENT_URI, new String[] {Data._ID, Phone.NUMBER, Phone.TYPE, Phone.LABEL}, Data.CONTACT_ID + "=?" + " AND " + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'", new String[] {String.valueOf(contactId)}, null);
Finding all Data of a given type for a given raw contact
-
Cursor c = getContentResolver().query(Data.CONTENT_URI, new String[] {Data._ID, Phone.NUMBER, Phone.TYPE, Phone.LABEL}, Data.RAW_CONTACT_ID + "=?" + " AND " + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'", new String[] {String.valueOf(rawContactId)}, null);
-