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.