短信的监听
来源:互联网 发布:主机屋的数据库名称 编辑:程序博客网 时间:2024/05/25 18:09
//----------------------------------------------------------------头文件--------------------------------------------------------------
#include <e32base.h>
#include <msvapi.h> // MMsvSessionObserver
#include <msvids.h> // Folder Ids
#include <smut.h> // KUidMsgTypeSMS
#include <txtrich.h> // CRichText
// CONSTANTS
const TInt KSmsMessageLength = 512;
const TInt KAddressLength = 64;
class MSmsEngineObserver
{
public:
virtual void MessageReceived(TDesC& aMsg, TDesC& aAddr) = 0;
};
class CYourSmsEngine : public CBase,
public MMsvSessionObserver
{
public:
static CYourSmsEngine* NewL(MSmsEngineObserver* aObserver);
static CYourSmsEngine* NewLC(MSmsEngineObserver* aObserver);
virtual ~CYourSmsEngine();
private:
void HandleSessionEventL(TMsvSessionEvent aEvent,
TAny* aArg1, TAny* aArg2, TAny* aArg3);
private:
void ConstructL();
CYourSmsEngine(MSmsEngineObserver* aObserver);
private:
// Observers SmsEngine states
MSmsEngineObserver* iObserver;
// Message body
TBuf<KSmsMessageLength> iMessage;
// Address (phonenumber)
TBuf<KAddressLength> iAddress;
// Session with the messaging server
CMsvSession* iMsvSession;
// CMsvEntry accesses and acts upon a particular Message Server entry
CMsvEntry* iMsvEntry;
// Id of a new message
TMsvId iNewMessageId;
// Id of the sent message
TMsvId iSentMessageId;
};
//---------------------------------------------------------------定义文件--------------------------------------------------------------
#include "yoursmsengine.h"
#ifdef __WINS__
const TMsvId KObservedFolderId = KMsvDraftEntryId;
#else
const TMsvId KObservedFolderId = KMsvGlobalInBoxIndexEntryId;
#endif
const TMsvId KInbox = KMsvGlobalInBoxIndexEntryId;
CYourSmsEngine* CYourSmsEngine::NewL(MSmsEngineObserver* aObserver)
{
CYourSmsEngine* self = NewLC(aObserver);
CleanupStack::Pop(self);
return self;
}
CYourSmsEngine* CYourSmsEngine::NewLC(MSmsEngineObserver* aObserver)
{
CYourSmsEngine* self = new (ELeave) CYourSmsEngine(aObserver);
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
void CYourSmsEngine::ConstructL()
{
// SMS automatic receiving needs a session to the messaging server
iMsvSession = CMsvSession::OpenAsyncL(*this);
}
CYourSmsEngine::CYourSmsEngine(MSmsEngineObserver* aObserver)
: iObserver(aObserver)
{
}
CYourSmsEngine::~CYourSmsEngine()
{
delete iMsvEntry;
if (iMsvSession)
iMsvSession->Cancel();
delete iMsvSession;
}
void CYourSmsEngine::HandleSessionEventL(
TMsvSessionEvent aEvent, TAny* aArg1, TAny* aArg2, TAny* /*aArg3*/)
{
switch (aEvent)
{
case EMsvServerReady:
{
// Initialise iMsvEntry
if (!iMsvEntry)
{
iMsvEntry = CMsvEntry::NewL(*iMsvSession, KInbox,
TMsvSelectionOrdering());
}
break;
}
case EMsvEntriesCreated:
{
// Only look for changes in the Inbox
if (aArg2 && *(static_cast<TMsvId*>(aArg2)) == KObservedFolderId)
{
CMsvEntrySelection* entries =
static_cast<CMsvEntrySelection*>(aArg1);
if( entries->Count() >= 1 )
{
iNewMessageId = entries->At(0);
}
else
{
return;
}
}
break;
}
case EMsvEntriesChanged:
{
// Look for changes. When using the emulator
// observed folder is drafts, otherwise inbox
if (aArg2 && *(static_cast<TMsvId*>(aArg2)) == KObservedFolderId)
{
CMsvEntrySelection* entries =
static_cast<CMsvEntrySelection*>(aArg1);
if(!entries && entries->Count() < 1 )
{
return;
}
else if (iNewMessageId == entries->At(0))
{
if( !iMsvEntry )
{
return;
}
// Set entry context to the new message
iMsvEntry->SetEntryL(iNewMessageId);
// Check the type of the arrived message and
// that the message is complete
// Only SMS's are our consern
if (iMsvEntry->Entry().iMtm != KUidMsgTypeSMS ||
!iMsvEntry->Entry().Complete())
{
return;
}
// Read-only store
CMsvStore* store = iMsvEntry->ReadStoreL();
CleanupStack::PushL(store);
// Get address of received message.
iAddress.Copy(iMsvEntry->Entry().iDetails);
// Body text
if (store->HasBodyTextL())
{
CRichText* richText = CRichText::NewL(
CEikonEnv::Static()->SystemParaFormatLayerL(),
CEikonEnv::Static()->SystemCharFormatLayerL());
CleanupStack::PushL(richText);
store->RestoreBodyTextL(*richText);
TPtrC ptr =
richText->Read(0, richText->DocumentLength());
iMessage.Copy(ptr);
CleanupStack::PopAndDestroy(richText);
CleanupStack::PopAndDestroy(store);
// Send message and phone number to caller
iObserver->MessageReceived(iMessage, iAddress);
}
else
{
CleanupStack::PopAndDestroy(store);
}
}
}
break;
}
default:
{
break;
}
}
}
- 短信的监听
- Android短信内容的监听
- android的日积月累-监听短信
- android 下的短信监听
- Android监听收到的短信
- ContentObserver监听发送的短信
- 监听接收到的短信
- Android中短信的收发机制 发送短信 接收短信 拦截短信 监听短信
- 监听短信
- 监听短信
- 短信监听
- 监听短信
- 短信监听
- 监听短信
- Android中读取短信信息,短信监听的各种问题,实现监听短信的功能
- 短信的监听与电话的呼出的监听
- Android监听发送短信和接受短信的事件
- 接受短信广播和监听短信数据库变化的方法
- C语言中产生随机数
- 配置文件验证管理员登陆 学习总结
- ASP.NET MVC 实践之路 之十八 利用ASP.NET MVC处理文件上传与下载
- test
- hadoop日志分析程序
- 短信的监听
- 郭德刚相声大全
- WebQQ协议分析(10)——聊天(3)
- JAVA 程序员情书
- Android开机自动启动程序设置
- C# 代理 事件 详解
- IDLE的运行、编辑与调试
- Oracle开发专题之:分析函数总结
- 创建链接文件——ln