Android Content Providers(三)——Contacts Provider

接着上篇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:

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'sLOOKUP_IDappended to it. The device's contacts app delegates read and write permissions to this content URI for the life of your activity. See theContent Provider Basicsguide 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'sAdd 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 theIntentargument, in the "data" field. To get the value, callgetData().

Edit a contact

ACTION_EDIT

CONTENT_LOOKUP_URIfor 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 clicksDoneto 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


关于这部分内容我还有些理解的不是很到位的地方,后续会继续学习,感兴趣的朋友可以一起研究。

欢迎关注我的新浪微博和我交流:@唐韧_Ryan


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值