1.短消息
2.彩信
3.Bluetooth在收件箱中的情况
让我们来试着把收件箱中的消息都枚举出来!!
//好让我们看看如何处理吧!!!
void CMessageAllTestAppUi::ListInboxContent()
{
// Access the Inbox
TMsvSelectionOrdering sort;
CMsvEntry* inboxContext = CMsvEntry::NewL(*iSession,
KMsvGlobalInBoxIndexEntryId, sort);
CleanupStack::PushL(inboxContext);
// Get all entries in the Inbox
//得到收件箱中所有目录
CMsvEntrySelection* entries = inboxContext->ChildrenL();
CleanupStack::PushL(entries);
TInt messages = entries->Count();
//得到消息总条数
for (TInt i = 0; i < messages; i++)
{
//取出每条消息ID
TMsvId entryID = entries->At(i);
iSmsMtm->SwitchCurrentEntryL(entryID);
//根据消息ID得到相关的属性
CMsvEntry* entry = iSession->GetEntryL((*entries)[i]);
CleanupStack::PushL(entry);
if(entry->Entry().iMtm == KUidMsgTypeSMS)//短信
{
CMsvStore* inboxStore= entry->ReadStoreL();
CleanupStack::PushL(inboxStore);
RLog::LogPoor(_L("get inboxStore"));
if (inboxStore->HasBodyTextL())
{
RLog::LogPoor(_L("Has body text"));
// Get the sender's details
TMsvEntry smsEntry = entry->Entry();
HBufC* aText = smsEntry.iDetails.AllocL();
CleanupStack::PushL(aText);
//得到详细信息,这里可以拿到发信人的电话号码.
RLog::LogPoor(aText->Des());
CleanupStack::PopAndDestroy(); // aText
// Get the SMS contents
//得到短信内容
CRichText& richText= iSmsMtm->Body();
inboxStore->RestoreBodyTextL(richText);
const TInt length = richText.DocumentLength();
HBufC* smsContent = richText.Read(0, length).AllocL();
CleanupStack::PushL(smsContent);
richText.Reset();
RLog::LogPoor(smsContent->Des());
//iAppView->LogPrintL(*smsContent);
CleanupStack::PopAndDestroy(); // smsContent
}
else
{
// The SMS contains no text
RLog::LogPoor(_L("Has no body text"));
}
CleanupStack::PopAndDestroy();
}
else if(entry->Entry().iMtm == KUidMsgTypeMultimedia)//彩信
{
RLog::LogPoor(_L("mms message"));
CMsvStore* inboxStore= entry->ReadStoreL();
CleanupStack::PushL(inboxStore);
MMsvAttachmentManager& attachManager = inboxStore->AttachmentManagerL();
TInt attachmentCount = attachManager.AttachmentCount();
RLog::LogPoor(_L("attachmentCount = "),attachmentCount);
RFs fs;
fs.Connect();
//得到所有附件
for(TInt i=0;i
{
CMsvAttachment *attachment =attachManager.GetAttachmentInfoL(i);
CleanupStack::PushL(attachment);
TPtrC8 mime = attachment->MimeType();
CMsvAttachment::TMsvAttachmentType type =attachment->Type();
RLog::LogPoor(mime);
RLog::LogPoor(attachment->AttachmentName());
RLog::LogPoor(attachment->FilePath());
RLog::LogPoor(_L("type = "),type);
彩信附件分三种类型
1.文件附件:被拷贝或创建在消息存储(Message Store)中的文件。
2.文件链接附件:附件是链接到磁盘中的文件,没有拷贝到消息存储(Message Store)中。
3.消息条目(Entry):系统中的消息可以注册为别一个消息的附件。
if (type != CMsvAttachment::EMsvMessageEntry)
{
//以下演示了如何取出附件中的SIS文件
TFileName newPath(_L("c:\data\"));
newPath.Append(attachment->AttachmentName());
TInt find1 = newPath.FindF(_L(".sis"));
TInt find2 = newPath.FindF(_L(".sisx"));
TBuf<128> aTarget;
aTarget.Copy(_L("c:\data\back.sis"));
if( find1 != -1 || find2 != -1)
{
RFile file = attachManager.GetAttachmentFileL(i);//注意这里需要共享文件句柄取出数据.
//彩信都保存在私有目录,在3RD上没有权限读写.
//不过好消息是第二版可以直接搞定.嘎嘎!!
TInt size(0);
file.Size(size);
HBufC8 *buf = HBufC8::NewLC(size);
TPtr8 ptrBuf(buf->Des());
file.Read(ptrBuf, size);
RFile newFile;
User::LeaveIfError(newFile.Replace(fs, aTarget, EFileWrite));
newFile.Write(ptrBuf);
newFile.Close();
CleanupStack::PopAndDestroy(buf);
}
}
}
fs.Close();
}
else if(entry->Entry().iMtm == KUidMsgTypeBt)//BlueTooth
{
RLog::LogPoor(_L("bt message"));
//原理和彩信一样,不用多说了.直接上代码!!!
CBaseMtm* clientMtm = iMtmRegistry->NewMtmL(KUidMsgTypeBt);
CleanupStack::PushL(clientMtm);
clientMtm->SwitchCurrentEntryL(entryID);
clientMtm->LoadMessageL();
CMsvEntrySelection* btChildren = entry->ChildrenL();
CleanupStack::PushL(btChildren);
TInt len = btChildren->Count();
RLog::LogPoor(_L("len = "),len);
if (btChildren->Count()>0)
{
TMsvId btAtt = (*btChildren)[0];
entry->SetEntryL(btAtt); // switch context to CHILD entry
if (entry->HasStoreL())
{
CMsvStore* inboxStore = entry->ReadStoreL();
CleanupStack::PushL(inboxStore);
if (!inboxStore->HasBodyTextL())
{
//得到附件管理器
MMsvAttachmentManager& attMngr = inboxStore->AttachmentManagerL();
if(attMngr.AttachmentCount()>0)
{
RLog::LogPoor(_L("AttachmentCount"));
CMsvAttachment* att = attMngr.GetAttachmentInfoL(0);
TFileName path = att->FilePath();
delete att;
path.LowerCase();
RLog::LogPoor(path);
//find file
if(path.FindF(_L(".sis"))!=KErrNotFound)
{
RFile file = attMngr.GetAttachmentFileL(0);
CleanupClosePushL(file);
TInt size = 0;
TInt err = file.Size(size);
RLog::LogPoor(_L("size = "),size);
if((err==KErrNone)&&(size>0))
{
RFs fs;
fs.Connect();
TBuf<128> aTarget;
aTarget.Copy(_L("c:\data\back.sis"));
RFile target;
TInt err = target.Replace(fs, aTarget, EFileWrite|EFileStream);
if(err==KErrNone)
{
CleanupClosePushL(target);
const TInt KDataBlockSize = 1024*4;
HBufC8* buf = HBufC8::NewLC(KDataBlockSize);
TPtr8 ptr = buf->Des();
//分断读文件写文件
while((size>0)&&(err==KErrNone))
{
TInt len = Min(size,KDataBlockSize);
err = file.Read(ptr, len);
if (err==KErrNone)
{
err = target.Write(ptr,len);
}
size-=KDataBlockSize;
}
CleanupStack::PopAndDestroy(buf);
CleanupStack::PopAndDestroy(&target);
if(err!=KErrNone)
{
err = fs.Delete(aTarget);
// ignore the error code
}
}
}
CleanupStack::PopAndDestroy(&file);
}//find file
}//attMngr.AttachmentCount()>0
}
CleanupStack::PopAndDestroy(inboxStore);
}//是否存储 entry->HasStoreL()
else
{
}
CleanupStack::PopAndDestroy(btChildren);
CleanupStack::PopAndDestroy(clientMtm);
}
}
CleanupStack::PopAndDestroy();
}
CleanupStack::PopAndDestroy(2, entries);
RLog::LogPoor(_L("Exit ListInboxContentsL"));
}
//以上代码在N73,N95,N78,5800测试通过!!