×
Namespaces

Variants
Actions

短信操作

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata

代码示例
文章
huwell 在 17 Dec 2008 创建
最后由 hamishwillee 在 14 Jun 2013 编辑

这篇文章演示了短信处理的各项操作。


Contents

前提

  • 按如下方式下载SmsHandler.zip:
  • 解压SmsHandler.zip得到SmsHandler.hSmsHandler.cpp
  • 拷贝粘贴SmsHandler.h到你工程目录的/inc目录下.
  • 拷贝粘贴SmsHandler.cpp到你功能目录的/src目录下.
  • 编辑你的.mmp文件增加SmsHandler.cppSOURCE模块.
源代码	SMSHandler.cpp


  • 编辑你的.mmp为短信处理增加必要的链接库


//Libraries included for SMS support-
LIBRARY msgs.lib smcm.lib gsmu.lib mtur.lib



  • 打开你的CYrApplicationContainer.h文件
  • SmsHandler.h包含进去


#include "SMSHandler.h" //Added for SMS Handling


  • 定义一个SmsHandler类对象


    private: //data
.......
.......
CSmsHandler* iSmsHandler;



  • 打开你的CYrApplicationContainer.cpp文件
  • 初始化SmsHanlder对象


CYrApplicationContainer::ConstructL()......
{
.....
.....
SetRect(aRect);
ActivateL();
 
iSmsHandler = CSmsHandler::NewL(); // SmsHandler
}

发送信息

  • 定义一个你自己的SendMsg()方法:


void CYrApplicationContainer ::SendMsg()
{
TBuf<128> SMSText,PhoneNumber;
SMSText.Copy(_L("Test Message"));
PhoneNumber.Copy(_L("999999999")); //Replace Number as per your needs
 
iSmsHandler->SendL( PhoneNumber, SMSText) ;
}



读取短信文件夹

收件箱

发件箱

草稿

发出的信息

  • 下列代码演示了如何从收件箱获取信息. 这里使用了KMsvGlobalInBoxIndexEntryId.
  • 发件箱中读取信息,使用KMsvGlobalOutBoxIndexEntryId
  • 草稿中读取信息,使用KMsvDraftEntryId
  • 发出的信息中读取信息,使用KMsvSentEntryId


void CSmsHandler::ReadInbox()
{
HBufC* SMSContent = HBufC::NewLC(400);
HBufC8* SMSContent8 = HBufC8::NewLC(400);
 
TMsvSelectionOrdering sort;
sort.SetShowInvisibleEntries(ETrue); // we want to handle also the invisible entries
 
CMsvEntry* inboxContext=CMsvEntry::NewL(*iSession,KMsvGlobalInBoxIndexEntryId,sort); // Reading Messages from Inbox Folder
CleanupStack::PushL(inboxContext);
 
CMsvEntrySelection* entries = inboxContext->ChildrenL();
CleanupStack::PushL( entries );
 
TInt msgCount= entries->Count();
for (TInt i=0; i<entries->Count(); i++)
{
 
TMsvId entryID = entries->At(i);
iSmsMtm->SwitchCurrentEntryL(entryID);
 
CMsvEntry* entry= iSession->GetEntryL((*entries)[i]);
CleanupStack::PushL(entry);
 
CMsvStore* inboxStore= entry->ReadStoreL();
CleanupStack::PushL(inboxStore);
if (inboxStore->HasBodyTextL())
{
TMsvEntry entry1 = entry->Entry();
TBufC<50> aText(entry1.iDetails); // Gives you phone Number
TBuf16<20> msg;
msg.Copy(aText);
 
CRichText& richText= iSmsMtm->Body();
inboxStore->RestoreBodyTextL(richText);
const TInt length = richText.DocumentLength();
 
SMSContent->Des().Copy(richText.Read(0,length)); // Gives you actual content(Body) of SMS
richText.Reset();
 
SMSContent8->Des().Copy(SMSContent->Des());
 
WriteToFile(SMSContent8->Des()); // Write SMS Body in the SMSBody.txt file
}
else
{
// no text in SMS
}
CleanupStack::PopAndDestroy(2,entry);
}
CleanupStack::PopAndDestroy(4,SMSContent);
}


//Note that "SMSBody.txt" used in the following code must be created beforehand as we are using Open function from RFile API
void CSmsHandler::WriteToFile(const TPtrC8& aSMSContent8)
{
_LIT(KFileSpec,"\\SMSBody.txt");//File, in which SMS Body will be stored
TInt pos=0;
RFs fs;
fs.Connect();
RFile file;
TInt err=file.Open(fs,KFileSpec,EFileWrite);
if(err==KErrNone)
{
file.Seek(ESeekEnd,pos);
file.Write(aSMSContent8);
file.Close();
}
fs.Close();
//File closed
}



读取新短信 ( 在线模式 )

获取信息内容

获取手机号码

  • 您可以在SMSHandler.cpp文件中找到MessageReceivedL()方法
  • 根据下列代码读取信息内容和手机发好


void CSmsHandler::MessageReceivedL( TMsvId aEntryId )
{
CMsvEntry* serverEntry = iSession->GetEntryL( aEntryId ); // current entry
CleanupStack::PushL( serverEntry );
TMsvEntry entry = serverEntry->Entry(); // currently handled message entry
 
entry.SetNew( ETrue );
entry.SetUnread( ETrue );
entry.SetVisible( ETrue );
 
serverEntry->ChangeL( entry ); // commit changes
 
//Added to retrieve message body
const TDesC& descp = entry.iDescription; // iDescription will have only first 32 characters from the message
TBuf8<40> MessageArrived;
MessageArrived.Copy(descp);
 
//Added to retrieve Phone Number of the Sender
iSmsMtm->SwitchCurrentEntryL(aEntryId);
iSmsMtm->LoadMessageL();
CSmsHeader& header = iSmsMtm->SmsHeader();
 
TPtrC from = header.FromAddress();
const TDesC& phoneNumber = from;
 
CleanupStack::PopAndDestroy( serverEntry );
}


从发送信息中删除短信( 在线模式 )

  • SmsHandler.cppHandleSessionEventL()中增加下列代码
  • 这样你可以发送信息,并从已发送的信息文件夹中删除它


case EMsvEntriesMoved:
{
// Entry id is obtained from the session event arguments.
TMsvId* entryId = STATIC_CAST( TMsvId*, aArg2 );
 
// We are interested in messages that are moved to Sent Item Folder
if ( *entryId == KMsvSentEntryId )
{
TMsvSelectionOrdering sort;
sort.SetSorting(EMsvSortByDateReverse);
sort.SetShowInvisibleEntries(ETrue); // we want to handle also the invisible entries
 
CMsvEntry* parentEntry = CMsvEntry::NewL(*iSession, KMsvSentEntryId, sort);
CleanupStack::PushL(parentEntry);
 
CMsvEntrySelection* entries = parentEntry->ChildrenL();
CleanupStack::PushL(entries);
 
for(TInt i = 0; i < entries->Count(); i++)
{
if( parentEntry->ChildDataL(entries->At(i)).iMtmData3 != KUidMsgTypeSMS.iUid )
{
parentEntry->DeleteL(entries->At(i));
break;
}
}
CleanupStack::PopAndDestroy( entries );
CleanupStack::PopAndDestroy( parentEntry );
}
break;
}
}



从短信文件夹中删除短信

收件箱

发件箱

草稿

发出的信息

  • 下列代码演示如何删除收件箱中的信息,使用了KMsvGlobalInBoxIndexEntryId
  • 下列代码演示如何删除发件箱中的信息,使用了KMsvGlobalOutBoxIndexEntryId
  • 下列代码演示如何删除草稿中的信息,使用了KMsvDraftEntryId
  • 下列代码演示如何删除发出的信息中的信息,使用了KMsvSentEntryId


void CSmsHandler::DeleteMessages()
{
TMsvSelectionOrdering sort;
sort.SetShowInvisibleEntries(ETrue); // we want to handle also the invisible entries
 
CMsvEntry* inboxContext=CMsvEntry::NewL(*iSession,KMsvGlobalInBoxIndexEntryId,sort);
CleanupStack::PushL(inboxContext);
 
CMsvEntrySelection* entries = inboxContext->ChildrenL();
CleanupStack::PushL( entries );
 
TInt msgCount= entries->Count();
TInt i;
for (i=0; i<entries->Count(); i++)
{
TMsvId entryID = entries->At(i);
iSmsMtm->SwitchCurrentEntryL(entryID);
 
CMsvEntry* entry= iSession->GetEntryL((*entries)[i]);
CleanupStack::PushL(entry);
 
entry->DeleteL(entries->At(i));
CleanupStack::PopAndDestroy(entry);
}
CleanupStack::PopAndDestroy(entries);
CleanupStack::PopAndDestroy(inboxContext);
}



取消" 发送情况报告 "

  • 打开SmsHandler.cpp增加SetDeliverReport = EFalseCreateMsgL()函数


TBool CSmsHandler::CreateMsgL()
{
.....
.....
settings->CopyL( iSmsMtm->ServiceSettings() ); // restore settings
settings->SetDelivery( ESmsDeliveryImmediately ); // to be delivered immediately
settings->SetDeliveryReport(EFalse);// Delivery Report Disabled here
header.SetSmsSettingsL( *settings ); // new settings
....
....
}

下列示例演示了如何获取和删除发送报告:File:SMS DeliveryReport Deleting.zip


发送信息到多个接收方

  • 打开SmsHandler.cppCreateMsgL()AddAddresseeL()方法中增加多个号码


TBool CSmsHandler::CreateMsgL()
{
........
// Recipient number is displayed also as the recipient alias.
entry.iDetails.Set( iRecipientNumber );
 
// Add addressee.
TBuf<15> PhoneNumber2;
PhoneNumber2.Copy(_L("9999999999")); //This is second number on which message will be sent
iSmsMtm->AddAddresseeL( iRecipientNumber, entry.iDetails );
iSmsMtm->AddAddresseeL( PhoneNumber2, entry.iDetails );
......
.......
}
This page was last modified on 14 June 2013, at 09:28.
89 page views in the last 30 days.
×