×
Namespaces

Variants
Actions
(Difference between revisions)

使用Qt Mobility Messaging API发送短信

From Nokia Developer Wiki
Jump to: navigation, search
flycarl (Talk | contribs)
flycarl (Talk | contribs)
Line 67: Line 67:
 
message.setTo(toList);
 
message.setTo(toList);
  
message.setBody(ui->lineEdit->text()); //发送短信,内容为lineEdit的字符,支持中文
+
message.setBody(ui->lineEdit->text()); //设置短信内容为lineEdit中的字符,支持中文
  
 
service.send(message);
 
service.send(message);

Revision as of 09:05, 18 November 2010

这演示如何用Qt Mobility的Messaging API发送短信,

Article Metadata
Tested with
Devices(s): Nokia 5800 and N8.
Compatibility
Platform(s): S60 3rd Edition, FP1, FP2
S60 5th Edition Symbian^3
Symbian
Article
Keywords: QMessageService, QMessage, Chinese
Created: (18 Nov 2010)
Last edited: flycarl (18 Nov 2010)


Contents

简介

这里的代码演示如何用Qt Mobility的Messaging API发送短信,自Qt Mobility 1.1.0可以发送中文短信。 代码需要以下能力 NetworkServices LocalServices ReadUserData WriteUserData UserEnvironment ReadDeviceData WriteDeviceData 。自签名不够用,至少需要 开发者签名 ,也可以用 Open Signed Online来测试。


准备条件

编辑pro文件

在pro文件中增加这些行

CONFIG += mobility
MOBILITY = messaging
 
symbian {
 
TARGET.CAPABILITY = NetworkServices \
LocalServices \
ReadUserData \
WriteUserData \
UserEnvironment \
ReadDeviceData \
WriteDeviceData
}

代码

下面的代码片段演示了如何发短信,当使用Mobility API 头文件时,需要使用QTM_USE_NAMESPACE,否则会找不到头文件;当前置声明时,需要用 QTM_BEGIN_NAMESPACEQTM_END_NAMESPACE 将前置声明“包围”,否则会出编译错误。

#include <QMessageService>
#include <QMessage>
 
// Use Qt Mobility API namespace
QTM_USE_NAMESPACE

这里的代码片段使用了一个 有预设字符的lineEdit来存放要发送的字符,可以是中文字符,这样做避免了关于字符编码的冗长讨论。实际操作中也不建议将中文字符硬编码在代码里,需要使用中文字符的地方应遵循Qt的国际化和本地化的操作。

QMessageService service;
 
QMessage message;
 
QMessageAddressList toList;
 
QMessageAddress::Type addrType(QMessageAddress::Phone);
 
toList.append(QMessageAddress(addrType, “13800000000);//电话号码//这里可以设置群以号码
 
message.setType(QMessage::Sms);//短信类型
 
message.setTo(toList);
 
message.setBody(ui->lineEdit->text()); //设置短信内容为lineEdit中的字符,支持中文
 
service.send(message);

下载Demo

参考链接

369 page views in the last 30 days.
×