现在的位置: 首页 > 综合 > 正文

symbian短信操作(读、删)

2013年10月01日 ⁄ 综合 ⁄ 共 6428字 ⁄ 字号 评论关闭

Contents
1 前提
2 发送信息
3 读取短信文件夹
3.1 收件箱
3.2 发件箱
3.3 草稿
3.4 发出的信息
4 读取新短信 ( 在线模式 )
4.1 获取信息内容
4.2 获取手机号码
5 从发送信息中删除短信( 在线模式 )
6 从短信文件夹中删除短信
6.1 收件箱
6.2 发件箱
6.3 草稿
6.4 发出的信息
7 取消" 发送情况报告 "
8 发送信息到多个接收方

前提
按如下方式下载SmsHandler.zip:
S60第二版适用 = SmsHander for S60 2nd.Zip
S60第三版使用 = SmsHander for S60 3rd.Zip
解压SmsHandler.zip得到SmsHandler.h和SmsHandler.cpp
拷贝粘贴SmsHandler.h到你工程目录的/inc目录下.
拷贝粘贴SmsHandler.cpp到你功能目录的/src目录下.
编辑你的.mmp文件增加SmsHandler.cpp到SOURCE模块.
源代码 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.cpp的HandleSessionEventL()中增加下列代码
这样你可以发送信息,并从已发送的信息文件夹中删除它
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 = EFalse到CreateMsgL()函数
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
....
....
}

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

发送信息到多个接收方
打开SmsHandler.cpp在CreateMsgL()的AddAddresseeL()方法中增加多个号码

TBool CSmsHandler::CreateMsgL()
{
........
// Recipient number is displayed also as the recipient alias.
entry.iDetails.Set( iRecipientNumber );

// Add addressee.
TBuf<15> PhoneNumber2;

//This is second number on which message will be sent
PhoneNumber2.Copy(_L("9999999999"));
iSmsMtm->AddAddresseeL( iRecipientNumber, entry.iDetails );
iSmsMtm->AddAddresseeL( PhoneNumber2, entry.iDetails );
......
.......
}
声明:本贴转载自Forum Nokia Wiki,仅供学习参考。原贴地址:http://wiki.forum.nokia.com/index.php/%E7%9F%AD%E4%BF%A1%E6%93%8D%E4%BD%9C

抱歉!评论已关闭.