Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

如何向电话簿添加联系人

From Wiki
Jump to: navigation, search
Article Metadata

兼容于
平台安全性
需要的签名: DevCert
能力: ReadDeviceData ReadUserData WriteDeviceData WriteUserData

文章
翻译:
hoolee
最后由 hamishwillee 在 09 Aug 2012 编辑

S60 3.2不建议使用此API,因此使用时要注意到这点。

在你的Symbian C++程序中有很多地方要添加联系人,例如SMS程序,呼叫控制程序。要做到这些你首先需要加载电话簿资源文件,具体如下:

RPbkViewResourceFile phonebookResource( *(CEikonEnv::Static()) );
phonebookResource.OpenL();

一般说来如果你想允许用户从电话簿对话框选取多个联系人,你需要使用CPbkMultipleEntryFetchDlg,并传递正确的参数在里面。同时需要将正确的Phonebook Contact Engine与对话框绑定(这里作者选取缺省的电话簿,你也可以显示你自己指定的电话簿数据库)代码看起来如下:

CPbkMultipleEntryFetchDlg::TParams params;
CleanupStack::PushL(params);
 
CPbkContactEngine* iPbkContactEngine =
CPbkContactEngine::NewL(&iEikonEnv->FsSession());
CleanupStack::PushL(iPbkContactEngine);
params.iContactView = &iPbkContactEngine->AllContactsView();
 
CPbkMultipleEntryFetchDlg* fetcher =
CPbkMultipleEntryFetchDlg::NewL(params, *iPbkContactEngine);
fetcher->SetMopParent(this);

之后执行这个对话框。在返回时,对话框将会给你一个联系人列表,你可以通过Contact Engine来单独访问名片夹,并处理它们:

TBuf<30> phoneNumber;
 
TInt paramCount = params.iMarkedEntries->Count();
// Get the selected contacts id array
for ( TInt i = 0; i < paramCount; ++i )
{
const TContactItemId cid = ( *params.iMarkedEntries )[i];
// Open the selected contact using Phonebook engine,
// choose correct number (launch list query if needed)
CPbkContactItem* pbkItem = iPbkContactEngine->ReadContactLC( cid );
TPbkContactItemField* tmp;
if ((tmp = pbkItem->FindField(EPbkFieldIdPhoneNumberMobile)) != NULL)
{
phoneNumber = tmp->Text();
if(phoneNumber.Length() > 0){
if(iData)
iAppView->AddNumber(phoneNumber);
}
}
 
CleanupStack::PopAndDestroy(1);//CPbkContactItem
}

如果联系人有多个号码,那么就要显示一个原型对话框对号码进行选择(如系统自带的短信程序)具体代码如下:

TBuf<30> phoneNumber;
 
TInt paramCount = params.iMarkedEntries->Count();
// Get the selected contacts id array
for ( TInt i = 0; i < paramCount; ++i )
{
TBuf<128> phoneNumber;
TBuf<128> phoneName;
TBool namefound = EFalse;
const TContactItemId cid = ( *params.iMarkedEntries )[i];
// Open the selected contact using Phonebook engine,
// choose correct number (launch list query if needed)
CPbkContactItem* pbkItem = iPbkContactEngine->ReadContactLC( cid );
//Using CPbkSmsAddressSelect dialog for user to choose between numbers
CPbkSmsAddressSelect* dlg = new (ELeave) CPbkSmsAddressSelect();
CPbkSmsAddressSelect::TParams addparams(*pbkItem);
TBool selected = dlg->ExecuteLD(addparams);
//If selected is ETrue, it means that user has selected some number, else user has pressed cancel
//Also, if contact item has only one number, then there is no popup shown to the user and returned value is ETrue
if(selected){
//Getting the selected field, i.e. Mobile, Mobile (Business),Tel, Tel. (Home)
//You can find the exact label of field by numtmp->Label()
const TPbkContactItemField* numtmp = addparams.SelectedField();
if(numtmp){
phoneNumber = numtmp->Text();
}
TPbkContactItemField* nametmp;
if ((nametmp = pbkItem->FindField(EPbkFieldIdFirstName)) != NULL)
{
phoneName = nametmp->Text();
if(phoneName.Length() > 0){
namefound = ETrue;
}
}
if ((nametmp = pbkItem->FindField(EPbkFieldIdLastName)) != NULL)
{
if(namefound){
phoneName.Append(_L(" "));
phoneName.Append(nametmp->Text());
}
else{
phoneName = nametmp->Text();
}
}
}
CleanupStack::PopAndDestroy(1);//CPbkContactItem
}

最后要清除相关资源

//CPbkContactEngine, CPbkMultipleEntryFetchDlg::TParams
CleanupStack::PopAndDestroy(2);
phonebookResource.Close();

需要的头文件

#include <rpbkviewresourcefile.h>
#include <cpbkmultipleentryfetchdlg.h>
#include <cpbkcontactengine.h>
#include <cpbkcontactitem.h>
#include <cpbksmsaddressselect.h>

需要的链接库

LIBRARY pbkview.lib pbkeng.lib

需要的能力

Capability ReadDeviceData ReadUserData WriteDeviceData WriteUserData
This page was last modified on 9 August 2012, at 05:13.
168 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×