Contact类

publicstaticclass
ContactsContract.Contacts
extendsObject
implementsBaseColumnsContactsContract.ContactOptionsColumnsContactsContract.ContactStatusColumnsContactsContract.ContactsColumns
java.lang.Object
android.provider.ContactsContract.Contacts
这里的Contact是个内部类.
ClassOverview
Constantsforthecontactstable,whichcontainsarecordperaggregateofrawcontactsrepresentingthesameperson.
对Contacts表共17项数据:

变量名列名备注
_ID_id
LOOKUP_KEYlookup
NAME_RAW_CONTACT_ID参照@1
DISPLAY_NAMEdisplay_name在Contacts对列的描述为DISPLAY_NAME_PRIMARY
PHOTO_IDphoto_id
IN_VISIBLE_GROUPin_visible_group
HAS_PHONE_NUMBERhas_phone_number
TIMES_CONTACTEDtimes_contacted
LAST_TIME_CONTACTEDlast_time_contacted
STARREDstarred
CUSTOM_RINGTONEcustom_ringtone
SEND_TO_VOICEMAILsend_to_voicemail
CONTACT_PRESENCEcontact_presence
CONTACT_STATUScontact_status
CONTACT_STATUS_TIMESTAMPcontact_status_ts
CONTACT_STATUS_RES_PACKAGEcontact_status_res_package
CONTACT_STATUS_LABELcontact_status_label
CONTACT_STATUS_ICONcontact_status_icon
注意1:在Contacts对列的描述有NAME_RAW_CONTACT_ID,但没找到相应的变量和列名。
可能ContentResolver觉得把该项暴露给用户每什么意义,就把它隐藏起来了。该项应该是只给系统内部用的哦。
注意2:只有五项是可写TIMES_CONTACTED,LAST_TIME_CONTACTED,STARRED,CUSTOM_RINGTONE,SEND_TO_VOICEMAIL.
Operations

数据插入
AContactcannotbecreatedexplicitly.Whenarawcontactisinserted,theproviderwillfirsttrytofindaContactrepresentingthesameperson.
Ifoneisfound,therawcontact'sCONTACT_IDcolumngetsthe_IDoftheaggregateContact.
Ifnomatchisfound,theproviderautomaticallyinsertsanewContact
andputsits_IDintotheCONTACT_IDcolumnofthenewlyinsertedrawcontact.
用户部能直接插入数据。数据的插入是由系统自动来完成的。

当rawcontact被插入或时,系统就会检查是否可以把该rawcontact加到已经有的组(contact)。
如果找到可以加入的组,就把组ID写到rawcontact的CONTACT_ID这个数据项中。
如果没找到就由系统重新创建一组,并把组ID写到rawcontact的CONTACT_ID这个数据项中。
如果rawcontact的structuredname,organization,phonenumber,emailaddress,ornickname被改变。

系统就会检查rawcontact是否还属于rawcontact的CONTACT_ID标记的那个组。
如果发现rawcontact不属于rawcontact的CONTACT_ID标记的那个组。
那么系统就会找是否可以把该rawcontact加到已经有的其他组(contact)
如果找到可以加入的组,就把组ID写到rawcontact的CONTACT_ID这个数据项中。
如果没找到就由系统重新创建一组,并把组ID写到rawcontact的CONTACT_ID这个数据项中。
注意:这的组是指contact(Aggregationofrawcontact)
数据更新
自动更新:当contact所包含的RawContacts的信息发生改变时,系统会对contact做相应的更新。
手动强制更新:
OnlycertaincolumnsofContactaremodifiable:TIMES_CONTACTED,LAST_TIME_CONTACTED,STARRED,CUSTOM_RINGTONE,SEND_TO_VOICEMAIL.ChanginganyofthesecolumnsontheContactalsochangesthemonallconstituentrawcontacts.
只有五项是可被用户直接更新。它们是TIMES_CONTACTED,LAST_TIME_CONTACTED,STARRED,CUSTOM_RINGTONE,SEND_TO_VOICEMAIL。
当它们被手动强制更新时,contact所包含的RawContacts的相应项也会被一起更新。
数据删除
Contacts文档说:
BecarefulwithdeletingContacts!Deletinganaggregatecontactdeletesallconstituentrawcontacts.
Thecorrespondingsyncadapterswillnoticethedeletionsoftheirrespectiverawcontacts
andremovethemfromtheirbackendstorage.
删除一个Contacts,会把它所包含所有rawcontacts都删除掉。
但是用下面语句居然删除不了任何Contacts.
getContentResolver().delete(Contacts.CONTENT_URI,null,
null);
不过在删除Contacts的所有rawcontacts后,Contacts也被删除了。
Query
*Ifyouneedtoreadanindividualcontact,considerusingCONTENT_LOOKUP_URIinsteadofCONTENT_URI.
*Ifyouneedtolookupacontactbythephonenumber,usePhoneLookup.CONTENT_FILTER_URI,whichisoptimizedforthispurpose.
*Ifyouneedtolookupacontactbypartialname,e.g.toproducefilter-as-you-typesuggestions,usetheCONTENT_FILTER_URIURI.
*Ifyouneedtolookupacontactbysomedataelementlikeemailaddress,nickname,etc,
useaqueryagainsttheContactsContract.Datatable.TheresultwillcontaincontactID,nameetc.
可用如下语句进行查询:
Cursorc=getContentResolver().query(Contacts.CONTENT_URI,null,
null,null,Contacts.DISPLAY_NAME+"COLLATENOCASE");
注意1:对于查单个contact信息时,可用CONTENT_LOOKUP_URI来进行查询
注意2:对于想通过电话号码查询时,可用PhoneLookup.CONTENT_FILTER_URI来查询。
具体参照《用PhoneLookup进行电话号码查询
注意3:对于想通过名字的模糊查询来查询时,可以CONTENT_FILTER_URIURI来查询。
注意4:对于想通过emailaddress,nickname来查询时,可查询ContactsContract.Data表。
它的查询结果里面包含了contactID,name等信息。具体可参考《rawcontact子表数据查询》
注意5:可以使用CONTENT_STREQUENT_URI来查询starred和经常联系的联系人信息
<wbr style="line-height:25px">关于Contact的Uri介绍.</wbr><wbr style="line-height:25px"><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">CONTENT_LOOKUP_UR</wbr></span><wbr style="line-height:25px">I<br style="line-height:25px"> Acontent://styleURIforthistablethatshouldbeusedtocreateshortcutsorotherwisecreatelong-termlinkstocontacts.<br style="line-height:25px"> ThisURIshouldalwaysbefollowedbya"/"andthecontact'sLOOKUP_KEY.Itcanoptionallyalsohavea"/"andlastknowncontactIDappendedafterthat.This"complete"formatisanimportantoptimizationandishighlyrecommended.<br style="line-height:25px"> Aslongasthecontact'srowIDremainsthesame,thisURIisequivalenttoCONTENT_URI.<br style="line-height:25px"> Ifthecontact'srowIDchangesasaresultofasyncoraggregation,thisURIwilllookupthecontactusingindirectinformation(syncIDsorconstituentrawcontacts).<br style="line-height:25px"> Lookupkeyshouldbeappendedunencoded-itisstoredintheencodedform,readyforuseinaURI.<br style="line-height:25px"> 例如下:<br style="line-height:25px"><span style="color:#0000ff; line-height:25px">content://com.android.contacts/contacts/lookup/0n/400</span><br style="line-height:25px"> 注意:"On"是lookUpKey,"400"是Contacts._ID<br style="line-height:25px"> 得到CONTENT_LOOKUP_URI的方式有:<br style="line-height:25px"> 1,从数据库中取得lookUpKey,然后再合成lookUpUri<br style="line-height:25px"><span style="color:#0000ff; line-height:25px">Cursorc=getContentResolver().query(Contacts.CONTENT_URI,null,<br style="line-height:25px"> null,null,Contacts.DISPLAY_NAME+"COLLATENOCASE");<br style="line-height:25px"> LongcontactId=cursor.getLong(cursor.getColumnIndex(Contacts._ID));<br style="line-height:25px"></span><span style="color:#808080; line-height:25px">//注意这里的lookUpKey是从Contacts表中取的</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">StringlookUpKey=cursor.getString(cursor.getColumnIndex(Contacts.LOOKUP_KEY));</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px"></span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">UrilookUpUri=Contacts.getLookupUri(contactId,lookUpKey);</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">Cursorc=context.getContentResolver().query(lookUpUri,null,</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">null,null,Contacts.DISPLAY_NAME+"COLLATENOCASE");</span><br style="line-height:25px"> 2,先用contactId合成contactUri,再用contactUri然后再合成lookUpUri.<br style="line-height:25px"><span style="color:#0000ff; line-height:25px">UricontactUri=ContentUris.withAppendedId(Contacts.CONTENT_URI,contactId);<br style="line-height:25px"> UrilookUpUri=Contacts.getLookupUri(context.getContentResolver(),contactUri);</span><br style="line-height:25px"> 另外可以从lookUpUri得到contactUri。<br style="line-height:25px"><span style="color:#993300; line-height:25px">publicstatic</span><span style="color:#0000ff; line-height:25px">Uri</span><span style="color:#ff6600; line-height:25px">lookupContact</span><span style="color:#0000ff; line-height:25px">(ContentResolverresolver,UrilookupUri)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Since:APILevel5</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">ComputesacontentURI(seeCONTENT_URI)givenalookupURI.</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Returnsnullifthecontactcannotbefound.</span><br style="line-height:25px"> 这里返回的是具体一个联系人的CONTENT_URI比如:content://com.android.contacts/contacts/400<br style="line-height:25px"><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">CONTENT_URI</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"> 全部contacts<br style="line-height:25px"> 全部contacts就是Contacts.CONTENT_URI(content://com.android.contacts/contacts)<br style="line-height:25px"> 某个人contacts<br style="line-height:25px"> 具体某个人就是Contacts.CONTENT_URI/contactId的形式。<br style="line-height:25px"> 比如:<br style="line-height:25px"> content://com.android.contacts/contacts/400<br style="line-height:25px"> 某个人contacts可用如下方式合成<br style="line-height:25px"> UricontactUri=ContentUris.withAppendedId(Contacts.CONTENT_URI,contactId);<br style="line-height:25px"> 也可用它对具体一个人进行查询<br style="line-height:25px"> Cursorc=context.getContentResolver().query(contentUri,null,<br style="line-height:25px"> null,null,Contacts.DISPLAY_NAME+"COLLATENOCASE");<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">CONTENT_FILTER_URI</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"> Thecontent://styleURIusedfor"type-to-filter"functionalityontheCONTENT_URIURI.<br style="line-height:25px"> Thefilterstringwillbeusedtomatchvariouspartsofthecontactname.<br style="line-height:25px"> ThefilterargumentshouldbepassedasanadditionalpathsegmentafterthisURI.<br style="line-height:25px"> 用于对名字进行模糊查询。<br style="line-height:25px"> Stringname="robin";<br style="line-height:25px"> Uriuri=Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI,Uri.encode(name));<br style="line-height:25px"> c=context.getContentResolver().query(uri,null,null,null,null);<br style="line-height:25px"><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">CONTENT_STREQUENT_URI</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"> Thecontent://styleURIforthistablejoinedwithusefuldatafromContactsContract.Data,<br style="line-height:25px"> filteredtoincludeonlystarredcontactsandthemostfrequentlycontactedcontacts.<br style="line-height:25px"> 它也是用于对联系人进行查询,但是只返回starred和经常联系的联系人信息。<br style="line-height:25px"> 比如:<br style="line-height:25px"> c=context.getContentResolver().query(Contacts.CONTENT_STREQUENT_URI,null,null,null,null);<br style="line-height:25px"> 返回的列和CONTENT_URI查询一样,也是17项数据。<br style="line-height:25px"><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">CONTENT_STREQUENT_FILTER_URI</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"> Thecontent://styleURIusedfor"type-to-filter"functionalityontheCONTENT_STREQUENT_URIURI.<br style="line-height:25px"> Thefilterstringwillbeusedtomatchvariouspartsofthecontactname.<br style="line-height:25px"> ThefilterargumentshouldbepassedasanadditionalpathsegmentafterthisURI.<br style="line-height:25px"> 它是用于通过名字来对联系人进行模糊查询,但是只返回starred和经常联系的联系人信息。<br style="line-height:25px"> 比如:<br style="line-height:25px"><span style="color:#0000ff; line-height:25px">Stringname="robin";<br style="line-height:25px"> Uriuri=Uri.withAppendedPath(Contacts.CONTENT_STREQUENT_FILTER_URI,Uri.encode(name));<br style="line-height:25px"> c=context.getContentResolver().query(uri,null,null,null,null);</span><br style="line-height:25px"> 返回的列和CONTENT_URI查询一样,也是17项数据。</wbr></wbr></wbr></wbr></wbr></wbr>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值