原贴http://blog.csdn.net/daiwenlove/archive/2009/12/18/5033871.aspx
/*
* VideoEggSmsEngine.h
*
* Created on: 2009-12-17
* Author: Administrator
*/
#ifndef VIDEOEGGSMSENGINE_H_
#define VIDEOEGGSMSENGINE_H_
#include <e32base.h>
#include <badesca.h>
#include <MSVAPI.H>
#include <eikenv.h>
#include <cmessagedata.h>
#include <sendui.h>
#include <SendUiConsts.h>
#include <gsmupdu.h>
class CMsvSession;
class CMsvEntry;
class CMsvEntrySelection;
class CClientMtmRegistry;
class CSmsClientMtm;
class CVideoEggSmsEngine:public CActive, public MMsvSessionObserver
{
public:
static CVideoEggSmsEngine* NewL();
virtual ~CVideoEggSmsEngine();
//from MMsvSessionObserver
public:
virtual void HandleSessionEventL(TMsvSessionEvent aEvent,TAny* aArg1,TAny* aArg2,TAny* aArg3);
public:
void SetMsgContent(const TDesC& aContent);
void SetToAddrress(const TDesC& aAddress);
void Start();
private:
CVideoEggSmsEngine();
void ConstructL();
void CreateMsg();
TBool ValidateSMS();
void SendMsg();
//from CActive
virtual void RunL();
virtual void DoCancel();
private:
CMsvSession* iSession;
CClientMtmRegistry* iMtmReg;
CSmsClientMtm* iSmsMtm;
CMsvOperation* iOper;
CMsvEntrySelection* iEntrySelection;
CMsvEntry* iMsvEntry;
TMsvId iSmsId;
TBuf<100> iMsgContent;
TBuf<11> iAddress;
};
#endif /* VIDEOEGGSMSENGINE_H_ */
/*
* VideoEggSmsEngine.cpp
*
* Created on: 2009-12-17
* Author: Administrator
*/
#include "VideoEggSmsEngine.h"
#include <smutset.h>
#include <BADESCA.H>
#include <MSVSTD.H>
#include <LOGCLI.H>
#include <LOGVIEW.H>
#include <SMSCLNT.H>
#include <MTCLREG.H>
#include <Msvids.h>
#include <tpbkcontactitemfield.h>
#include <cpbkcontactitem.h>
#include <Smut.h>
//#include <SMUTHDR.H>
#include <smuthdr.h>
#include <mtmdef.h>
#include <txtrich.h>
#include <e32std.h>
CVideoEggSmsEngine* CVideoEggSmsEngine::NewL()
{
CVideoEggSmsEngine* self=new(ELeave)CVideoEggSmsEngine;
CleanupStack::PushL(self);
self->ConstructL();
CleanupStack::Pop(self);
return self;
}
CVideoEggSmsEngine::CVideoEggSmsEngine():CActive(EPriorityStandard)
{
}
CVideoEggSmsEngine::~CVideoEggSmsEngine()
{
if(iOper)
{delete iOper;iOper=NULL;}
if(iEntrySelection)
{delete iEntrySelection;iEntrySelection=NULL;}
if(iMsvEntry)
{delete iMsvEntry;iMsvEntry=NULL;}
if(iSmsMtm)
{delete iSmsMtm;iSmsMtm=NULL;}
if(iMtmReg)
{delete iMtmReg;iMtmReg=NULL;}
if(iSession)
{delete iSession;iSession=NULL;}
}
void CVideoEggSmsEngine::ConstructL()
{
iSession=CMsvSession::OpenSyncL(*this);
iMtmReg=CClientMtmRegistry::NewL(*iSession);
iSmsMtm=static_cast<CSmsClientMtm*>(iMtmReg->NewMtmL(KUidMsgTypeSMS));
TMsvSelectionOrdering sort;
iMsvEntry = CMsvEntry::NewL(*iSession,KMsvGlobalInBoxIndexEntryId,sort);
iEntrySelection = iMsvEntry->ChildrenL();
iEntrySelection = new (ELeave) CMsvEntrySelection;
CActiveScheduler::Add(this);
}
void CVideoEggSmsEngine::Start()
{
CreateMsg();
if(ValidateSMS())
SendMsg();
else
CEikonEnv::InfoWinL(_L("some error "),_L("about message!"));
}
void CVideoEggSmsEngine::CreateMsg()
{
TMsvEntry indexEntry;
indexEntry.iDate.UniversalTime();
indexEntry.SetInPreparation(ETrue);
indexEntry.iMtm=KUidMsgTypeSMS;
indexEntry.iType=KUidMsvMessageEntry;
indexEntry.iServiceId=iSmsMtm->ServiceId();
iSmsMtm->SwitchCurrentEntryL(KMsvDraftEntryId);
iSmsMtm->Entry().CreateL(indexEntry);
iSmsId=indexEntry.Id();
iSmsMtm->SwitchCurrentEntryL(iSmsId);
CRichText& body=iSmsMtm->Body();
body.Reset();
body.InsertL(0,iMsgContent);
indexEntry.iDescription.Set(iMsgContent);
iSmsMtm->AddAddresseeL(iAddress);
indexEntry.iDetails.Set(iAddress);
iSmsMtm->Entry().ChangeL(indexEntry);
iSmsMtm->SaveMessageL();
}
TBool CVideoEggSmsEngine::ValidateSMS()
{
TMsvPartList msgCheckParts=
KMsvMessagePartBody|
KMsvMessagePartRecipient|
KMsvMessagePartOriginator|
KMsvMessagePartDate;
TMsvPartList msgFailParts=iSmsMtm->ValidateMessage(msgCheckParts);
if(msgFailParts==KMsvMessagePartNone)
return ETrue;
else
return EFalse;
}
void CVideoEggSmsEngine::SendMsg()
{
iSmsMtm->SwitchCurrentEntryL(iSmsId);
iSmsMtm->LoadMessageL();
CSmsSettings& settings=iSmsMtm->ServiceSettings();
TInt numSCAddresses=settings.ServiceCenterCount();
if(numSCAddresses>0)
{
TInt index=0;
index=settings.DefaultServiceCenter();
if((index<0)||(index>=numSCAddresses))
index=0;
TPtrC serverCenterNum=settings.GetServiceCenter(index).Address();
iSmsMtm->SmsHeader().SetServiceCenterAddressL(serverCenterNum);
}
CSmsSettings* sendOptions=CSmsSettings::NewL();
CleanupStack::PushL(sendOptions);
sendOptions->CopyL(iSmsMtm->ServiceSettings());
sendOptions->SetDelivery(ESmsDeliveryImmediately);
sendOptions->SetCharacterSet(TSmsDataCodingScheme::ESmsAlphabetUCS2);
iSmsMtm->SmsHeader().SetSmsSettingsL(*sendOptions);
CleanupStack::PopAndDestroy(sendOptions);
iSmsMtm->SaveMessageL();
TMsvEntry indexEntry=iSmsMtm->Entry().Entry();
indexEntry.SetInPreparation(EFalse);
indexEntry.SetSendingState(KMsvSendStateWaiting);
iSmsMtm->Entry().ChangeL(indexEntry);
Cancel();
iEntrySelection->Reset();
iEntrySelection->AppendL(iSmsId);
iMsvEntry=iSession->GetEntryL(KMsvDraftEntryId);
iOper=iMsvEntry->CopyL(iSmsMtm->Entry().EntryId(),iSmsMtm->ServiceId(),iStatus);
SetActive();
}
void CVideoEggSmsEngine::RunL()
{
if(iOper)
{delete iOper;iOper=NULL;}
if(iMsvEntry)
{delete iMsvEntry;iMsvEntry=NULL;}
if(iStatus==KErrNone)
{
CEikonEnv::InfoWinL(_L("send "),_L("success!"));
}
else
CEikonEnv::InfoWinL(_L("send "),_L("Fail!"));
}
void CVideoEggSmsEngine::DoCancel()
{
}
void CVideoEggSmsEngine::SetMsgContent(const TDesC& aContent)
{
iMsgContent.Zero();
iMsgContent.Append(aContent);
}
void CVideoEggSmsEngine::SetToAddrress(const TDesC& aAddress)
{
iAddress.Zero();
iAddress.Append(aAddress);
}
void CVideoEggSmsEngine::HandleSessionEventL(TMsvSessionEvent aEvent,TAny* aArg1,TAny* aArg2,TAny* aArg3)
{}
调用的接口如下(eg):
_LIT(KSMSAddress,"13600001111");
_LIT(KSMSContent,"test Test ,Hello");
TBuf<15> address(KSMSAddress);
TBuf<120> content(KSMSContent);
CVideoEggSmsEngine* smsEngine=CVideoEggSmsEngine::NewL();
smsEngine->SetToAddrress(address);
smsEngine->SetMsgContent(content);
smsEngine->Start();
缺点:
1.没有实现群发功能
2.没有回调
3.没有注释
系统需要添加的库文件:
LIBRARY msgs.lib sendui.lib etext.lib gsmu.lib smcm.lib
能力:
CAPABILITY NetworkServices ReadDeviceData ReadUserData UserEnvironment WriteDeviceData WriteUserData SwEvent
不知道到底需要哪些能力,就加了好多