×
Namespaces

Variants
Actions

读取存储在SIM卡上的短消息

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata

兼容于
平台安全性
需要的签名: Self-Signed
能力: ReadUserData WriteUserData NetworkServices

文章
翻译:
chenziteng
最后由 hamishwillee 在 14 Jun 2013 编辑

Contents

概述

本示例代码可以读取存储在SIM卡上的短消息。

前提条件

使用本示例代码的类必须继承自MMsvSessionObserver

MMP文件

// .mmp
...
CAPABILITY ReadUserData WriteUserData NetworkServices
...
LIBRARY msgs.lib // CMsvSession
LIBRARY smcm.lib // TSmsUtilities
LIBRARY gsmu.lib // CSmsPDU
...

头文件

// .h
...
#include <msvapi.h> // MMsvSessionObserver
...
protected: // MMsvSessionObserver
void HandleSessionEventL(TMsvSessionEvent aEvent, TAny* aArg1, TAny* aArg2, TAny* aArg3);
...

源文件

// .cpp
...
#include <MTCLREG.h> // CClientMtmRegistry
#include <smsclnt.h> // CSmsClientMtm
#include <smscmds.h> // ESmsMtmCommandEnumeratePhoneStores
#include <TXTRICH.H> // CRichText
...
_LIT(KFileName, "e:\\simcardx.txt");
RFs& fs = CCoeEnv::Static()->FsSession();
TUint mode = EFileWrite+EFileStreamText+EFileShareAny;
RFileWriteStream file;
User::LeaveIfError(file.Replace(fs, KFileName, mode));
CleanupClosePushL(file);
file.WriteUint16L(0xFEFF); // Windows unicode 文件头
 
CMsvSession* msvs = CMsvSession::OpenAsyncL(*this);
CleanupStack::PushL(msvs);
CClientMtmRegistry* reg = CClientMtmRegistry::NewL(*msvs);
CleanupStack::PushL(reg);
CSmsClientMtm* mtm = static_cast<CSmsClientMtm*>(reg->NewMtmL(KUidMsgTypeSMS));
CleanupStack::PushL(mtm);
TMsvId imFolderId=0;
{
// 读取存储在SIM卡上的短消息,并将它们拷贝到消息存储中SMS服务下的一个隐藏目录中。
// 如果成功,那么imFolderId返回隐藏目录的ID
CMsvEntry* serviceEntry = msvs->GetEntryL(KMsvRootIndexEntryId);
CleanupStack::PushL(serviceEntry);
TMsvId serviceId;
TSmsUtilities::ServiceIdL(*serviceEntry, serviceId, KUidMsgTypeSMS);
CMsvEntrySelection* selection = new (ELeave) CMsvEntrySelection();
CleanupStack::PushL(selection);
selection->AppendL(serviceId);
TBuf8<1> emp (KNullDesC8);
CMsvOperationActiveSchedulerWait* waiter = CMsvOperationActiveSchedulerWait::NewLC();
CMsvOperation* operation = mtm->InvokeAsyncFunctionL(ESmsMtmCommandEnumeratePhoneStores, *selection, emp, waiter->iStatus);
CleanupStack::PushL(operation);
waiter->Start();
User::LeaveIfError(waiter->iStatus.Int());
TSmsProgressBuf progressBuf;
progressBuf.Copy(operation->ProgressL());
CleanupStack::PopAndDestroy(operation);
TSmsProgress progress = progressBuf();
if (!progress.iError)
{
// 隐藏目录的ID
simFolderId = progress.iEnumerateFolder;
}
CleanupStack::PopAndDestroy(waiter);
CleanupStack::PopAndDestroy(selection);
CleanupStack::PopAndDestroy(serviceEntry);
}
{
// 遍历隐藏目录中所有的短消息项,并将它们导出到一个文本文件中。
// and output their info to a text file
TMsvSelectionOrdering sort;
sort.SetShowInvisibleEntries(ETrue); // 处理标计为不可见的项。
CMsvEntry* inboxContext=CMsvEntry::NewL(*msvs,simFolderId ,sort);
CleanupStack::PushL(inboxContext);
CMsvEntrySelection* entries = inboxContext->ChildrenL();
CleanupStack::PushL( entries );
TInt count=entries->Count();
for (TInt i=0; i<count; i++)
{
TMsvId entryID = entries->At(i);
mtm->SwitchCurrentEntryL(entryID);
CMsvEntry* entry= msvs->GetEntryL(entryID);
CleanupStack::PushL(entry);
TMsvEntry entry1 = entry->Entry();
CMsvStore* inboxStore= entry->ReadStoreL();
CleanupStack::PushL(inboxStore);
// 如果该项是非空的短消息
if ((entry1.iMtm==KUidMsgTypeSMS)&&(inboxStore->HasBodyTextL()))
{
mtm->LoadMessageL();
// the message body
CRichText& richText= mtm->Body();
inboxStore->RestoreBodyTextL(richText);
const TInt length = richText.DocumentLength();
TPtrC body = richText.Read(0,length);
if(body.Length()>0)
{
_LIT(KComma, ",");
_LIT(KCrLf, "\x0D\x0A");
_LIT(KFormat, "%F%Y-%M-%D %H:%T:%S.%C");
// 发件人的电话号码或者电话簿中的名字(如果电话簿中记录了本号码的话)
file.WriteL(entry1.iDetails);
file.WriteL(KComma);
 
// 发件人的电话号码
TPtrC number = mtm->SmsHeader().FromAddress();
file.WriteL(number);
file.WriteL(KComma);
 
// 消息的接收时间
TBuf<32> buf;
entry1.iDate.FormatL(buf, KFormat);
file.WriteL(buf);
file.WriteL(KComma);
 
file.WriteL(body);
 
file.WriteL(KCrLf);
}
}
CleanupStack::PopAndDestroy(inboxStore);
CleanupStack::PopAndDestroy(entry);
}
CleanupStack::PopAndDestroy(entries);
CleanupStack::PopAndDestroy(inboxContext);
}
CleanupStack::PopAndDestroy(mtm);
CleanupStack::PopAndDestroy(reg);
CleanupStack::PopAndDestroy(msvs);
 
CleanupStack::PopAndDestroy(&file);

译者注:如果不喜欢代码中的中文注释可以从本文的英文版中拷贝代码 Reading messages stored on SIM card using Symbian C++

相关文档: Deleting messages stored on SIM card using Symbian C++

This page was last modified on 14 June 2013, at 09:29.
152 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×