Android Content Providers(二)——Contacts Provider
Contacts Provider是Android中一個強大并且靈活的組件,負(fù)責(zé)管理系統(tǒng)通訊錄的數(shù)據(jù),對外提供訪問接口來對系統(tǒng)通訊錄進行訪問和操作。
以下是Contacts Provider的組織結(jié)構(gòu)圖:
可以看出Android的系統(tǒng)通訊錄是三層架構(gòu),通過URI進行訪問。下面看一下每一層的類結(jié)構(gòu)和官方解釋:
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.
Contacts表包含了不同的聯(lián)系人的記錄,RawContacts表是聯(lián)系人的數(shù)據(jù)集合,指定用戶賬號和類型,Data表是存儲具體的聯(lián)系人信息,包括郵件、電話號碼等。
首先來看看Row Contacts,以下是官方解釋:
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 a 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.
大致意思是Row contact存儲了用戶數(shù)據(jù)所在服務(wù)器的賬號和賬號類型,由于Contacts Provider允許多個在線服務(wù)作為一個聯(lián)系人的數(shù)據(jù)源,同時也允許用戶使用同一個服務(wù)但是不同的賬號來存儲聯(lián)系人信息。可以理解成Row Contacts存儲的是用戶的賬戶信息,包括賬戶類型和賬戶名稱等。下面看看Row Contacts的表結(jié)構(gòu):
| Column name | Use | Notes |
| ACCOUNT_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 addresses. See the next entry forACCOUNT_TYPEfor more information. | The format of this name is specific to its account type. It is not necessarily an email address. |
| ACCOUNT_TYPE | 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. |
| DELETED | 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. |
另外,官方文檔中還給出了一點說明: ACCOUNT_NAME存儲用戶的賬戶名,比如sanpleuser@gmail.com,ACCOUNT_TYPE是賬戶類型,比如如果是Google賬戶則存儲的值就是com.google。
Most of the data for a raw contact isn't stored in theContactsContract.RawContactstable. Instead, it's stored in one or more rows in theContactsContract.Datatable. Each data row has a columnData.RAW_CONTACT_IDthat contains theRawContacts._IDvalue of its parentContactsContract.RawContactsrow.
大部分raw contacts的數(shù)據(jù)并沒有直接存儲在RowContacts表中,而是以一行或多行的形式存儲在了Data表中,每一行有一列Data.RAW_CONTACT_ID包含了一個指向RawContacts表的列RawContacts._ID,也就是說RawContacts表存儲的是引用。為了更好的理解Row Contacts是如何工作的,文檔中舉了一個例子:
To understand how raw contacts work, consider the user "Emily Dickinson" who has the following three user accounts defined on her device:
- emily.dickinson@gmail.com
- emilyd@gmail.com
- Twitter account "belle_of_amherst"
This user has enabledSync Contactsfor all three of these accounts in theAccountssettings.
Suppose Emily Dickinson opens a browser window, logs into Gmail asemily.dickinson@gmail.com, opens Contacts, and adds "Thomas Higginson". Later on, she logs into Gmail asemilyd@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:
大致意思就是說一個用戶擁有三個賬號,兩個是gmail賬號,一個是twitter賬號,然后登陸其中一個gmail賬號并添加了一個聯(lián)系人,然后他又登陸另一個gmail賬號并且發(fā)了一封郵件給之前添加的聯(lián)系人,隨后變自動把這個聯(lián)系人添加到了當(dāng)前登陸的這個gmail賬戶里,并且在twitter上關(guān)注了這個聯(lián)系人。隨后在RawContacts里面便添加了三條數(shù)據(jù),第一行是以第一個gmail賬號登陸的賬戶名,賬戶類型是Google,第二行數(shù)據(jù)是以第二個gmail賬戶登陸的賬戶名,賬戶類型仍然為Google,第三行是Twitter的賬號,賬號類型為Twitter。
接下來看看Data是如何存儲數(shù)據(jù)的,關(guān)于Data的說明,官方文檔給出了如下解釋:
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 in theContactsContract.Datatable. To help manage this, theContactsContract.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.
不同的數(shù)據(jù)類型存儲在一張表中,名字、電話號碼、郵箱、地址或者照片等都存儲在ContactsContract.Data表中,為了方便管理數(shù)據(jù),Data表有一些描述性的列,還有其他一些一般的列。描述性列的內(nèi)容不管數(shù)據(jù)的類型都有同樣的意思,但是一般性的列的內(nèi)容就會依據(jù)不同的數(shù)據(jù)類型有不同的意思。
Some examples of descriptive column names are:
RAW_CONTACT_ID
和RawContact的_ID對應(yīng)的值
MIMETYPE
The type of data stored in this row, expressed as a custom MIME type. The Contacts Provider uses the MIME types defined in the subclasses ofContactsContract.CommonDataKinds. These MIME types are open source, and can be used by any application or sync adapter that works with the Contacts Provider.
MIMETYPE定義了數(shù)據(jù)的類型,通過CommonDataKinds指定數(shù)據(jù)的類型。
Generic column names
There are 15 generic columns namedDATA1throughDATA15that are generally available and an additional four generic columnsSYNC1throughSYNC4that should 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.
從DATA1到DATA15有15個字段來存儲值,DATA1一般是索引列,Contacts Provider通常用這個索引列來進行目標(biāo)查詢。例如,在郵件行中,這一列存儲實際的郵件地址。另外,DATA15是用來存儲BLOB(Binary Large Object)類型數(shù)據(jù)的,比如用戶頭像。
這有一個例子來說明ContactsContract.CommonDataKinds;
For example, theContactsContract.CommonDataKinds.Emailclass defines type-specific column name constants for aContactsContract.Datarow that has the MIME typeEmail.CONTENT_ITEM_TYPE. The class contains the constantADDRESSfor the email address column. The actual value ofADDRESSis "data1", which is the same as the column's generic name.
通過下面的圖可以有一個比較直觀的認(rèn)識:
CommonDataKinds.Email存儲了字段名常量,而對應(yīng)的ContactsContract.Data則存儲了實際的值,二者通過RAW_CONTACT_ID對應(yīng)同一個RawContact.這里DATA1存儲的就是Address(郵件地址)
以下是其他一些CommonDataKinds的類型:
| Mapping class | Type of data | Notes |
| 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 groups. |
未完待續(xù)。。。
歡迎關(guān)注我的新浪微博和我交流:@唐韌_Ryan
總結(jié)
以上是生活随笔為你收集整理的Android Content Providers(二)——Contacts Provider的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux的显示器设置
- 下一篇: linux系统串口dmx512,基于st