http://wiki.forum.nokia.com/index.php/%E7%9F%AD%E4%BF%A1%E6%93%8D%E4%BD%9C
這篇文章示範了簡訊處理的各項操作。
Contents
[hide]
- 1 前提
- 2 發送資訊
- 3 讀取簡訊檔案夾
- 3.1 收件匣
- 3.2 寄件匣
- 3.3 草稿
- 3.4 發出的資訊
- 4 讀取新簡訊 ( 線上模式 )
- 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
//Libraries included for SMS support-
LIBRARYmsgs.lib smcm.lib gsmu.lib mtur.lib
- 開啟你的CYrApplicationContainer.h檔案
- 將SmsHandler.h包含進去
#include "SMSHandler.h" //Added for SMS Handling
private: //data
.......
.......
CSmsHandler* iSmsHandler;
- 開啟你的CYrApplicationContainer.cpp檔案
- 初始化SmsHanlder對象
CYrApplicationContainer::ConstructL()......
{
.....
.....
SetRect(aRect);
ActivateL();
iSmsHandler = CSmsHandler::NewL(); // SmsHandler
}
發送資訊
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
....
....
}
下列樣本示範了如何擷取和刪除發送報告:File:SMS DeliveryReport Deleting.zip
發送資訊到多個接收方
- 開啟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;
PhoneNumber2.Copy(_L("9999999999")); //This is second number on which message will be sent
iSmsMtm->AddAddresseeL( iRecipientNumber, entry.iDetails );
iSmsMtm->AddAddresseeL( PhoneNumber2, entry.iDetails );
......
.......
}