android中ContactsContract类的使⽤
1.加⼊读写权限
联系⼈信息Uri:
content://ts/contacts
联系⼈电话Uri:
content://ts/data/phones
联系⼈EmailUri:
content://ts/data/emails
(推荐)也可以这样获取联系⼈信息Uri:Uriuri=T_URI;
2.查询与添加联系⼈的操作(单元测试⽤例)
publicclassContactTestextendsAndroidTestCa
{
privatestaticfinalStringTAG="ContactTest";
publicvoidtestGetAllContact()throwsThrowable
{
//获取联系⼈信息的Uri
Uriuri=T_URI;
//获取ContentResolver
ContentResolvercontentResolver=text().getContentResolver();
//查询数据,返回Cursor
Cursorcursor=(uri,null,null,null,null);
while(Next())
{
StringBuildersb=newStringBuilder();
//获取联系⼈的ID
StringcontactId=ing(umnIndex(ts._ID));
//获取联系⼈的姓名
Stringname=ing(umnIndex(Y_NAME));
//构造联系⼈信息
("contactId=").append(contactId).append(",Name=").append(name);
//查询电话类型的数据操作
Cursorphones=(T_URI,
null,
T_ID+"="+contactId,
null,null);
while(Next())
{
StringphoneNumber=ing(umnIndex(
));
//添加Phone的信息
(",Phone=").append(phoneNumber);
}
();
//查询Email类型的数据操作
Cursoremails=(T_URI,
null,
T_ID+"="+contactId,
null,null);
while(Next())
{
StringemailAddress=ing(umnIndex(
));
//添加Email的信息
(",Email=").append(emailAddress);
}
();
Log.i(TAG,ng());
}
();
}
publicvoidtestInrt()
{
ContentValuesvalues=newContentValues();
//⾸先向T_URI执⾏⼀个空值插⼊,⽬的是获取系统返回的rawContactId
UrirawContactUri=text().getContentResolver().inrt(T_URI,values);
//获取id
longrawContactId=d(rawContactUri);
//往data表⼊姓名数据
();
(_CONTACT_ID,rawContactId);//添加id
(PE,T_ITEM_TYPE);//添加内容类型(MIMETYPE)
(_NAME,"凯风⾃南");//添加名字,添加到firstname位置
text().getContentResolver().inrt(T_URI,values);
//往data表⼊电话数据
();
(_CONTACT_ID,rawContactId);
(PE,T_ITEM_TYPE);
(,"");
(,_MOBILE);
text().getContentResolver().inrt(T_URI,values);
//往data表⼊Email数据
();
(_CONTACT_ID,rawContactId);
(PE,T_ITEM_TYPE);
(,"kenhoo@");
(,_WORK);
text().getContentResolver().inrt(T_URI,values);
}
publicvoidtestSave()throwsThrowable
{
//官⽅⽂档位置:reference/android/provider/
//建⽴⼀个ArrayList存放批量的参数
ArrayList
intrawContactInrtIndex=();
(ert(T_URI)
.withValue(T_TYPE,null)
.withValue(T_NAME,null)
.build());
//官⽅⽂档位置:reference/android/provider/
//withValueBackReference后退引⽤前⾯联系⼈的id
(ert(T_URI)
.withValueBackReference(_CONTACT_ID,rawContactInrtIndex)
.withValue(PE,T_ITEM_TYPE)
.withValue(_NAME,"⼩明")
.build());
(ert(T_URI)
.withValueBackReference(_CONTACT_ID,rawContactInrtIndex)
.withValue(PE,T_ITEM_TYPE)
.withValue(,"")
.withValue(,_MOBILE)
.withValue(,"⼿机号")
.build());
(ert(T_URI)
.withValueBackReference(_CONTACT_ID,rawContactInrtIndex)
.withValue(PE,T_ITEM_TYPE)
.withValue(,"ken@")
.withValue(,_WORK)
.build());
ContentProviderResult[]results=text().getContentResolver()
.applyBatch(ITY,ops);
for(ContentProviderResultresult:results)
{
Log.i(TAG,ng());
}
}
}*******************************************************************************************************
这⾥主要使⽤的是
ContactsContract类
从Android2.0SDK开始有关联系⼈provider的类变成了ContactsContract,虽然⽼的ts能⽤,但是在SDK中标记为
为deprecated将被放弃不推荐的⽅法,⽽从Android2.0及APILevel为5开始新增了tsContract来代替原来的⽅法。不
过Android123表⽰⼤家做两⼿准备,毕竟⽬前70%的设备以及Ophone1.0和1.5均不⽀持ContactsContract。
ts中的所有字段
cts实现了4个接⼝,并从4个接⼝中,继承了不同的字段,⼀共有23个如下:
_CONTACTED="times_contacted"
Thenumberoftimesacontacthasbeencontacted
T_STATUS="contact_status"
Contact'slateststatusupdate.
_RINGTONE="custom_ringtone"
ormissing,thedefaultringtoneisud.
_PHONE_NUMBER="has_phone_number"
Anindicatorofwhetherthiscontacthasatleastonephonenumber."1"ifthereisatleastonephonenumber,"0"otherwi.
IC_NAME="phonetic_name"
PronunciationofthefullnameinthephoneticalphabetspecifiedbyPHONETIC_NAME_STYLE.
IC_NAME_STYLE="phonetic_name_style"
ThephoneticalphabetudtoreprentthePHONETIC_neticNameStyle.
T_STATUS_LABEL="contact_status_label"
TheresourceIDofthelabeldescribingthesourceofcontactstatus,e.g."GoogleTalk".Thisresourceisscopedby
theCONTACT_STATUS_RES_PACKAGE.
_KEY="lookup"
Anopaquevaluethatcontainshintsonhowtofindthecontactifitsrowidchangedasaresultofasyncoraggregation.
T_STATUS_ICON="contact_status_icon"
sourceisscopedbythe
CONTACT_STATUS_RES_PACKAGE.
_TIME_CONTACTED="last_time_contacted"
Thelasttimeacontactwascontacted.
Y_NAME="display_name"
Thedisplaynameforthecontact.
_KEY_ALTERNATIVE="sort_key_alt"
Sortkeybadonthealternativereprentationofthefullname,DISPLAY_NAME_rWesternnames,itisthe
oneusingthe"familynamefirst"format.
_VISIBLE_GROUP="in_visible_group"
LookupvaluethatreflectstheGROUP_embershipforthiscontact.
ts._ID="_id"
TheuniqueIDforarow.
D="starred"
Isthecontactstarred?
_KEY_PRIMARY="sort_key"
Sortkeythattakesintoaccountlocale-badtraditionsforsortingnamesinaddressbooks.
Y_NAME_ALTERNATIVE="display_name_alt"
Analternativereprentationofthedisplayname,suchas"familynamefirst"insteadof"givennamefirst"
alternativeisnotavailable,thevaluesshouldbethesameasDISPLAY_NAME_PRIMARY
T_PRESENCE="contact_prence"
Updatesforindividualstatusdefinitions.
Y_NAME_SOURCE="display_name_source"
Thekindofdatathatisudasthedisplaynameforthecontact,
:convertDisplayNameSourcestoalinkafteritisun-hidden
T_STATUS_RES_PACKAGE="contact_status_res_package"
Thepackagecontainingresourcesforthisstatus:labelandicon.
T_STATUS_TIMESTAMP="contact_status_ts"
Theabsolutetimeinmillicondswhenthelateststatuswasinrted/updated.
_ID="photo_id"
Referencetotherowinthedatatableholdingthephoto.
_TO_VOICEMAIL="nd_to_voicemail"
ing,defaultstofal.
可以⽤以下⽅法,列出ts中的所有字段:
privatevoidlistColumnNames()
{
privateUricontactUri=T_URI;
ContentResolverresolver=tentResolver();
Cursorcursor=(contactUri,null,null,null,null);
intcolumnNumber=umnCount();
for(inti=0;i
{
Stringtemp=umnName(i);
Log.e("listColumnNames",""+i+"t"+temp);
}
();
}
本文发布于:2022-11-27 15:56:00,感谢您对本站的认可!
本文链接:http://www.wtabcd.cn/fanwen/fan/90/31904.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |