如何从电话本添加联系人

来源:互联网 发布:windows 2000在线音乐 编辑:程序博客网 时间:2024/05/01 23:28

How to add contacts from phonebook
如何从电话本添加联系人

 

Note:API is deprecated in S60 3.2 so while using this API do consider this
在S60 3.2中不建议使用

 

There are plenty of occasions when you need to add contacts from Phonebook to your Symbian C++ application.
Examples are SMS, Call Control applications etc.
To do that you have to first load the phonebook's resource file like

有很多时候你需要从电话本中添加联系人,比如:短信、电话控制程序等。
要做这些,你必须先载入电话本资源:

 

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

 

In general you would want to allow users to fetch multiple entries from the phonebook dialog
so you can use CPbkMultipleEntryFetchDlg and pass the correct parameters.
 Also attach the correct Phonebook Contact Engine to the dialog.
(Here I am picking the default contact book,
whereas you may want to display your own special contact book database.) The code may look like

一般来说,你想允许用户能从电话本对话框得到多个入口,所以你可以使用CPbkMultipleEntryFetchDlg ,然后传入正确的参数。
同时将正确的电话本联系人添加到对话框(在这里我使用默认的联系人本,你可能项显示你自己特定的联系人数据库)代码如下:

 

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);

 

Later just execute the dialog. On return, the dialog will give you a list of contact ids with
which you can access individually selected contacts from Contact Engine and process them
接下来只要执行对话框,返回时,对话框将给你一个联系人ID列表,这样你能够单独从联系人引擎单独选择和执行他们

 

TBuf<30> phoneNumber;
 
TInt paramCount = params.iMarkedEntries->Count();
// Get the selected contacts id array // 得到选择的联系人ID数组
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
}

 

If you want to implement the behavior that, if a contact item has more than one number,
and you want to show native dialog to choose between the numbers(like in native Sms application),
you can use following code
如果你项实现这样的行为:如果一个联系人项有多个号码,你想在本程序显示给用户来选择这些号码(如一个短信程序),
你可以使用以下代码:

 

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 对话框给用户选择号码
 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

 // 如果selected是ETrue,表明用户选择了某个号码,否则用户取消了选择。同样如果联系人项只有一个号码,就没有弹出菜单给用户,返回值仍然为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()

  //得到选择的字段,如移动、移动(办公)、电话、家庭电话,通过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
}

 

In the end clean up your stack and close phonebook resource
最后清理你的栈,关闭电话本资源

 

//CPbkContactEngine, CPbkMultipleEntryFetchDlg::TParams
CleanupStack::PopAndDestroy(2);
phonebookResource.Close();
=======================================================================================================
Headers required
#include <rpbkviewresourcefile.h>
#include <cpbkmultipleentryfetchdlg.h>
#include <cpbkcontactengine.h>
#include <cpbkcontactitem.h>
#include <cpbksmsaddressselect.h>

Library needed
LIBRARY   pbkview.lib pbkeng.lib

Capability required
Capability  ReadDeviceData ReadUserData WriteDeviceData WriteUserData

 

原创粉丝点击