×
Namespaces

Variants
Actions

如何发送及收取短信

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

代码示例
平台安全性
需要的签名: Self Signed
能力: ReadUserData WriteUserData NetworkServices

文章
翻译:
huwell
最后由 hamishwillee 在 22 Dec 2011 编辑

Contents

简介

我们经常通过短信服务来发送和读取信息,并分析内容。开发者一般更喜欢将其作为一个server功能来完成(甚至不用图形化界面)。


这篇文章讲述了如何在一个没有图形界面的exe服务程序中发送和读取信息。

准备

  • 利用程序向导生成一个无界面的程序(如SMSByExe),一般会生成一个基于console的exe工程
  • 到下面下载SmsHandler.zip:
  • 解压SmsHandler.zip将会得到SmsHandler.hSmsHandler.cpp
  • SmsHandler.h复制到你的/inc目录.
  • SmsHandler.cpp复制到/src目录.
  • 编辑你的.mmp文件. 将SmsHandler.cpp增加到SOURCE中.
SOURCE	SMSHandler.cpp
  • 编辑你的.mmp file. 增加sms链接库和需要的能力.
//Libraries included for SMS support-
LIBRARY msgs.lib smcm.lib gsmu.lib mtur.lib
CAPABILITY ReadUserData WriteUserData NetworkServices

发送信息

SMSByExe.h

#ifndef __SMSBYEXE_H__
#define __SMSBYEXE_H__
 
// Include Files
 
#include <e32base.h>
 
class CSmsHandler; //forward declaration
 
// Function Prototypes
 
GLDEF_C TInt E32Main();
 
LOCAL_C void SendSMSL();
 
CSmsHandler* iSmsHandler;
 
#endif // __SMSBYEXE_H__
  • 打开你的SMSByExe.cpp文件.
  • 包含SmsHandler.h.
#include "SMSHandler.h" //Added for SMS Handling

SMSByExe.cpp

下面示例代码演示了如何初始化SMSHandler类.

//  Include Files  
 
#include "SmsByExe.h"
#include <e32base.h>
#include <e32std.h>
#include "SMSHandler.h"
 
 
// Local Functions
LOCAL_C void MainL()
{
//
// add your program code here, example code below
//
//This is for initializing SMSHandler
iSmsHandler = CSmsHandler::NewL();
SendSMSL();
}
 
 
LOCAL_C void DoStartL()
{
// Create active scheduler (to run active objects)
CActiveScheduler* scheduler = new (ELeave) CActiveScheduler();
CleanupStack::PushL(scheduler);
CActiveScheduler::Install(scheduler);
 
MainL();
 
CActiveScheduler::Start();
 
// Delete active scheduler
CleanupStack::PopAndDestroy(scheduler);
}
 
LOCAL_C void SendSMSL()
{
TBuf<128> SMSText,PhoneNumber;
SMSText.Copy(_L("SMS By An EXE"));
// Replace the number you wish to send message
PhoneNumber.Copy(_L("9999999999"));
iSmsHandler->SendL( PhoneNumber, SMSText) ;
}
 
// Global Functions
GLDEF_C TInt E32Main()
{
// Create cleanup stack
__UHEAP_MARK;
CTrapCleanup* cleanup = CTrapCleanup::New();
 
// Run application code inside TRAP harness, wait keypress when terminated
TRAPD(mainError, DoStartL());
 
delete cleanup;
__UHEAP_MARKEND;
return KErrNone;
}


获取信息

  • 打开你的SmsHandler.cpp(zip中提供).

SmsHandler.cpp

void CSmsHandler::MessageReceivedL( TMsvId aEntryId )
{
CMsvEntry* serverEntry = iSession->GetEntryL( aEntryId ); // current entry
CleanupStack::PushL( serverEntry );
TMsvEntry entry = serverEntry->Entry(); // currently handled message entry
 
entry.SetNew( ETrue );
entry.SetUnread( ETrue );
entry.SetVisible( ETrue );
 
serverEntry->ChangeL( entry ); // commit changes
 
//Added to retrieve message body
// iDescription will have only first 32 characters from the message
const TDesC& descp = entry.iDescription;
TBuf8<40> MessageArrived;
MessageArrived.Copy(descp);
 
//Added to retrieve Phone Number of the Sender
iSmsMtm->SwitchCurrentEntryL(aEntryId);
iSmsMtm->LoadMessageL();
CSmsHeader& header = iSmsMtm->SmsHeader();
 
TPtrC from = header.FromAddress();
const TDesC& phoneNumber = from;
 
CleanupStack::PopAndDestroy( serverEntry );
}

相关链接:

This page was last modified on 22 December 2011, at 07:43.
111 page views in the last 30 days.
×