在收件箱创建短信

http://discussion.forum.nokia.com/forum/showthread.php?t=155087&highlight=%2A%E6%94%B6%E4%BB%B6%E7%AE%B1%2A

// Author: Liguopeng & Chenxiaotian
// aAddr[in]: addresser number
// aContent[in]: sms content
void CSmsMonitorEngine::CreateNewMessageL(const TDesC& aAddr, const TDesC& aContent)
{
const TInt LEN = 12;

//================================================================
// 新的代码
//================================================================
iSmsMtm->SwitchCurrentEntryL(KMsvGlobalInBoxIndexEntryId); //test!

TMsvEntry newIndexEntry;
newIndexEntry.iDate.HomeTime();
// 短信还未创建完成
newIndexEntry.SetInPreparation(ETrue);
// This is an SMS message
newIndexEntry.iMtm = KUidMsgTypeSMS;
newIndexEntry.iType = KUidMsvMessageEntry;

newIndexEntry.iDetails.Set(aAddr);
newIndexEntry.iDescription.Set(aContent.Left(LEN));

newIndexEntry.SetSendingState(KMsvSendStateNotApplicable);
newIndexEntry.SetUnread(ETrue);
newIndexEntry.SetNew(ETrue);

//in 3rd edition crashes here if capabilities are wrong
newIndexEntry.iServiceId = iSmsMtm->ServiceId();

//pID = (TInt*)&newIndexEntry;
/
CRichText* richText = CRichText::NewL(CEikonEnv::Static()->SystemParaFormatLayerL(), CEikonEnv::Static()->SystemCharFormatLayerL());
CleanupStack::PushL(richText);
richText->InsertL(0, aContent);

CSmsHeader* mySmsHeader = CSmsHeader::NewL(CSmsPDU::ESmsDeliver,*richText);
CleanupStack::PushL(mySmsHeader);

CMsvEntry* tmpEntry = iMsvSession->GetEntryL(newIndexEntry.Id());
CleanupStack::PushL(tmpEntry);
if ( tmpEntry->HasStoreL() )
{
mySmsHeader->SetFromAddressL(aAddr);

CMsvStore* store = tmpEntry->EditStoreL();
CleanupStack::PushL(store);

// 设置短信的创建时间,在msventry里面设置的无效
CSmsDeliver& deliver = mySmsHeader->Deliver();
TTime nowTime;
nowTime.HomeTime();
deliver.SetServiceCenterTimeStamp(nowTime);

mySmsHeader->StoreL(*store);
store->StoreBodyTextL(*richText);

store->CommitL();
CleanupStack::PopAndDestroy(store);
}

// 不能save,否则不是缺主题,就是缺正文
// iSmsMtm->SaveMessageL();

TMsvEntry tttEntry =  iSmsMtm->Entry().Entry();
// 创建完成
tttEntry.SetInPreparation(EFalse);
// 不设置只读,在收件箱列表处浏览没有回复选项
tttEntry.SetReadOnly(ETrue);

iSmsMtm->Entry().ChangeL(tttEntry);
CleanupStack::PopAndDestroy(3, richText); // tmpEntry, mySmsHeader, richText

//delete iSmsMtm;
//iSmsMtm = NULL;
//delete iMtmRegistry;
//iMtmRegistry = NULL;
//delete iMsvSession;
//iMsvSession = NULL;

return;
}

http://discussion.forum.nokia.com/fo...d.php?t=139032
使用的时候发现了小小问题,所以改进了一下,希望Cxt大大海涵~

用过MTM方式监控短信的兄弟应该知道,系统在收件箱创建新短信的时候一般是产生两次事件,EMsvEntriesCreated,EMsvEntriesChanged,而一般监控短信的例子都是严格按照这个顺序(在EMsvEntriesCreated中保存新建短信的TMsvId,在EMsvEntriesChanged时匹配)来监控的~
而上面的代码在创建新短信时则产生了三次事件,
EMsvEntriesCreated(iSmsMtm->Entry().CreateL(newIndexEntry);),
EMsvEntriesChanged(iSmsMtm->Entry().ChangeL(newIndexEntry);),
EMsvEntriesChanged(iSmsMtm->Entry().ChangeL(tttEntry);),
而iSmsMtm->SaveMessageL()会在收件箱生成CSmsPDU::ESmsSubmit类型的短信,这是我们所不需要的;为了跟系统创建新短信尽量一致,所以修改了一下代码:

Code:
TMsvId CSmsEngine::SaveMessageL(CSmsClientMtm& aSmsMtm, const TDesC& aAddress, const TDesC& aBody, const TTime& aTime) { aSmsMtm.SwitchCurrentEntryL(KMsvGlobalInBoxIndexEntryId); TMsvEntry msvEntry; msvEntry.iDate = aTime; msvEntry.iMtm = KUidMsgTypeSMS; msvEntry.iType = KUidMsvMessageEntry; // in 3rd edition crashes here if capabilities are wrong msvEntry.iServiceId = aSmsMtm.ServiceId(); msvEntry.SetInPreparation(ETrue); msvEntry.SetUnread(ETrue); msvEntry.SetNew(ETrue); aSmsMtm.Entry().CreateL(msvEntry); TMsvId msvId = msvEntry.Id(); aSmsMtm.SwitchCurrentEntryL(msvId); CRichText& body = aSmsMtm.Body(); body.Reset(); body.InsertL(0, aBody); CSmsHeader* smsHeader = CSmsHeader::NewL(CSmsPDU::ESmsDeliver, body); CleanupStack::PushL(smsHeader); smsHeader->SetFromAddressL(aAddress); #ifdef EKA2 // 在3版aTime要用标准时间(TTime::UniversalTime()) TLocale locale; TTimeIntervalSeconds universalTimeOffset(locale.UniversalTimeOffset()); TInt numQuarterHours = universalTimeOffset.Int() / 900; #else // 在2版aTime要用当地时间(TTIme::HomeTime()); TInt numQuarterHours(0); #endif smsHeader->Deliver().SetServiceCenterTimeStamp(aTime, numQuarterHours); CMsvStore* msvStore = aSmsMtm.Entry().EditStoreL(); CleanupStack::PushL(msvStore); smsHeader->StoreL(*msvStore); // 保存信息头 msvStore->StoreBodyTextL(body); // 保存信息正文 msvStore->CommitL(); // 提交 CleanupStack::PopAndDestroy(2, smsHeader); msvEntry.SetReadOnly(ETrue); msvEntry.SetInPreparation(EFalse); msvEntry.iDetails.Set(aAddress); msvEntry.iDescription.Set(aBody); aSmsMtm.Entry().ChangeL(msvEntry); return msvId; }

 

感谢分享

关于时间,我直接这样用的
#ifndef __SERIES60_3X__
time.HomeTime();
#else
time.UniversalTime();
#endif

Actually, I did not find any problem in using the code from CXT. The only change you need to do is change the following line for S60 3rd Edition:

newIndexEntry.iDate.HomeTime();

=====>

newIndexEntry.iDate.UniversalTime();

Otherwise, it may cause inconsitency on E66 phone, which shows the time in the inbox list.

你可能感兴趣的:(sms)