下面是增加和查找的例子,需要加上头文件和在mmp里面加上响应的能力和lib,可以到网上找下
增加例子:
TBool CInformationSynView::AddContactItem()
{
_LIT(KFName,"kkkkk");
_LIT(KLName,"kkkkk");
_LIT(KNumber,"13777777778");
_LIT(QKUidContactFieldGivenName,"1111");
_LIT(QKUidContactFieldFamilyName,"2222");
_LIT(QKUidContactFieldCompanyName,"3333");
_LIT(QKUidContactFieldJobTitle,"4444");
_LIT(QKUidContactFieldPhoneNumber,"5555");
_LIT(QKUidContactFieldFax,"6666");
_LIT(QKUidContactFieldSIPID,"7777");
_LIT(QKUidContactFieldEMail,"8888");
//运用引擎打开默认通讯录
CPbkContactEngine* iPbkContactEngine = CPbkContactEngine::NewL();
//新建一空通信录项
CPbkContactItem* contact = iPbkContactEngine->CreateEmptyContactL();
CleanupStack::PushL(contact); //2
//设置first name 域
TPbkContactItemField* field = contact->FindField(EPbkFieldIdFirstName);
field->TextStorage()->SetTextL(QKUidContactFieldGivenName);
//设置last name域//2222
field = contact->FindField(EPbkFieldIdLastName);
field->TextStorage()->SetTextL(QKUidContactFieldFamilyName);
//设置CompanyName 域//3333
field = contact->FindField(EPbkFieldIdCompanyName);
field->TextStorage()->SetTextL(QKUidContactFieldCompanyName);
//设置JobTitle 域//4444
field = contact->FindField(EPbkFieldIdJobTitle);
field->TextStorage()->SetTextL(QKUidContactFieldJobTitle);
//设置PhoneNumberMobile //5555
field = contact->FindField(EPbkFieldIdPhoneNumberMobile);
field->TextStorage()->SetTextL(QKUidContactFieldPhoneNumber);
//设置Fax 域//6666
field = contact->FindField(EPbkFieldIdFaxNumber);
field->TextStorage()->SetTextL(QKUidContactFieldFax);
//设置IdSIPID 域//7777
// field = contact->FindField(EPbkFieldIdSIPID);
// if(field!=NULL){
// field->TextStorage()->SetTextL(QKUidContactFieldSIPID);
// }
//设置QKUidContactFieldEMail//8888
field = contact->FindField(EPbkFieldIdEmailAddress);
field->TextStorage()->SetTextL(QKUidContactFieldEMail);
//可以添加其他值域
//...
//修改后结果添加到数据库中,并返回这个通信录项的id,该id可以以后使用
TContactItemId Id = iPbkContactEngine->AddNewContactL(*contact);
CleanupStack::PopAndDestroy(contact);
return ETrue;
}
查找的例子
TBool CInformationSynView::SearchContactItem()
{//const TDesC& aTelephoneNumber
_LIT(GivenName,"1111");
_LIT(FamilyName,"2222");
_LIT(PhoneNumber,"5555");
TBuf<64> givenname(GivenName);
TBuf<64> familyname(FamilyName);
TBuf<64> phonenumber(PhoneNumber);
CContactDatabase* contactsDb = CContactDatabase::OpenL();
CleanupStack::PushL(contactsDb);
CPbkContactEngine* iPbkContactEngine1 = CPbkContactEngine::NewL();
CleanupStack::PushL(iPbkContactEngine1);
CContactIdArray* contactIdArray =iPbkContactEngine1-> MatchPhoneNumberL(phonenumber,phonenumber.Length());
CleanupStack::PushL(contactIdArray);
TInt matchnumber=contactIdArray->Count();
// iPbkContactEngine1->MContactDbObserver
if (contactIdArray->Count() > 0)
{
for(TInt i=0; i<matchnumber; i++)
{
TContactItemId ownCardId = (*contactIdArray)[i];
CContactItem* ownCard=contactsDb->ReadContactL(ownCardId);
CleanupStack::PushL(ownCard);
CContactItemFieldSet& currentFieldSet =ownCard->CardFields();
TInt GivenNameFieldId=currentFieldSet.Find(KUidContactFieldGivenName);
TInt FamilyNameFieldId=currentFieldSet.Find(KUidContactFieldFamilyName);
if( GivenNameFieldId!=KErrNotFound && FamilyNameFieldId!=KErrNotFound )
{ //读出数据
TPtrC name =ownCard->CardFields()[GivenNameFieldId].TextStorage()->Text();
TPtrC fName =ownCard->CardFields()[FamilyNameFieldId].TextStorage()->Text();
TInt nint=givenname.CompareC(name);
TInt fint=familyname.CompareC(fName);
if(nint!=0&&fint!=0){
return AddContactItem();
}
}
CleanupStack::PopAndDestroy(ownCard);
}
}
delete contactIdArray;
CleanupStack::PopAndDestroy(contactIdArray);
CleanupStack::PopAndDestroy(iPbkContactEngine1);
CleanupStack::PopAndDestroy(contactsDb);
return EFalse;
}