短信操作(转)

http://wiki.forum.nokia.com/index.php/%E7%9F%AD%E4%BF%A1%E6%93%8D%E4%BD%9C

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

Contents
[ hide]
前提
  • 按如下方式下载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 );
......
.......
}

转载于:https://www.cnblogs.com/yaoliang11/archive/2011/06/09/2076273.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值