接著上篇Android Content Providers(二)——Contacts Provider繼續,接下來要說明的是頂層的Contacts,Contacts是彙總連絡人表,在之前討論的RawContacts是原始連絡人表,在Android通訊錄的架構中,分為彙總連絡人、原始連絡人和資料表三層。資料表主要儲存資料和與原始連絡人相關聯的ID,Data表的欄位Data在之前已經說明過,原始連絡人RawContacts不直接儲存資料,資料通過ID關聯儲存在資料表Data中,而關於彙總連絡人,這裡要先講一下Android中的賬戶,Android可以綁定多個賬戶,在每一個賬戶下都可以有一份連絡人資訊,但是當我們進入通訊錄,看到的其實就是彙總連絡人,如果我們有多個賬戶,每個賬戶裡都有同一個連絡人,我們不希望看到同一個連絡人重複出現很多次,所有有了彙總連絡人表Contacts,Contacts同樣通過ID與原始連絡人表進行關聯。下面看一下這三者之間的一個關係圖:
頂層顯示的是彙總連絡人表Contacts,中間是原始連絡人表RawContacts,這裡有三個賬戶(兩個Gmail,一個Twitter)分別儲存這這個連絡人的資訊,最底層是Data表,儲存著連絡人的具體資訊。通過這個圖可以比較直觀的看出Android系統通訊錄的三層架構。
在使用過程中可以通過Intent的方式來進行通訊錄的修改操作,通過下面這張表可以看到有幾種直接調用系統銅須路的操作方式:
Task |
Action |
Data |
MIME type |
Notes |
Pick a contact from a list |
ACTION_PICK |
One of:
- Contacts.CONTENT_URI, which displays a list of contacts.
- Phone.CONTENT_URI, which displays a list of phone numbers
for a raw contact.
- StructuredPostal.CONTENT_URI, which displays a
list of postal addresses for a raw contact.
- Email.CONTENT_URI, which displays a list of email addresses
for a raw contact.
|
Not used |
Displays a list of raw contacts or a list of data from a raw contact, depending on the content URI type you supply. CallstartActivityForResult(), which returns the content URI of the selected row. The form of the URI is the table's content URI with the row's LOOKUP_IDappended to it. The device's contacts app delegates read and write permissions to this content URI for the life of your activity. See the Content Provider Basics guide for more details. |
Insert a new raw contact |
Insert.ACTION |
N/A |
RawContacts.CONTENT_TYPE, MIME type for a set of raw contacts. |
Displays the device's contacts application's Add Contactscreen. The extras values you add to the intent are displayed. If sent withstartActivityForResult(), the content URI of the newly-added raw contact is passed back to your activity'sonActivityResult()callback method in the Intentargument, in the "data" field. To get the value, callgetData(). |
Edit a contact |
ACTION_EDIT |
CONTENT_LOOKUP_URI for the contact. The editor activity will allow the user to edit any of the data associated with this contact. |
Contacts.CONTENT_ITEM_TYPE, a single contact. |
Displays the Edit Contact screen in the contacts application. The extras values you add to the intent are displayed. When the user clicksDone to save the edits, your activity returns to the foreground. |
這裡提供一種通過Intent來使用的方法,還有其他的使用方式,具體使用方式如下:
// Gets values from the UI
String name = mContactNameEditText.getText().toString();
String phone = mContactPhoneEditText.getText().toString();
String email = mContactEmailEditText.getText().toString();
String company = mCompanyName.getText().toString();
String jobtitle = mJobTitle.getText().toString();
// Creates a new intent for sending to the device's contacts application
Intent insertIntent =newIntent(ContactsContract.Intents.Insert.ACTION);
// Sets the MIME type to the one expected by the insertion activity
insertIntent.setType(ContactsContract.RawContacts.CONTENT_TYPE);
// Sets the new contact name
insertIntent.putExtra(ContactsContract.Intents.Insert.NAME,
name);
// Sets the new company and job title
insertIntent.putExtra(ContactsContract.Intents.Insert.COMPANY,
company);
insertIntent.putExtra(ContactsContract.Intents.Insert.JOB_TITLE,
jobtitle);
最後,在使用系統連絡人的時候,需要加上兩個許可權:
<uses-permission android:name="android.permission.READ_CONTACTS">
<uses-permission android:name="android.permission.WRITE_CONTACTS">
詳細文檔可以參考:http://developer.android.com/guide/topics/providers/contacts-provider.html
關於這部分內容我還有些理解的不是很到位的地方,後續會繼續學習,感興趣的朋友可以一起研究。
加入我們的QQ群或公眾帳號請查看:Ryan's
zone公眾帳號及QQ群
歡迎關注我的新浪微博和我交流:@唐韌_Ryan