×
Namespaces

Variants
Actions
(Difference between revisions)

使用Qt Mobility Messaging API发送短信

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Hamishwillee - Bot fixing redirect link.(Moving links from forum.nokia.com TO developer.nokia.com))
hamishwillee (Talk | contribs)
m (Hamishwillee - Bot change of template (Template:CodeSnippet) - now using Template:ArticleMetaData)
Line 1: Line 1:
{{CodeSnippet
+
{{ArticleMetaData
 
|id=...
 
|id=...
 
|platform=S60 3rd Edition, FP1, FP2<br>S60 5th Edition Symbian^3
 
|platform=S60 3rd Edition, FP1, FP2<br>S60 5th Edition Symbian^3
Line 7: Line 7:
 
|creationdate=Nov 18, 2010
 
|creationdate=Nov 18, 2010
 
|keywords= QMessageService, QMessage, Chinese
 
|keywords= QMessageService, QMessage, Chinese
 +
 +
|sourcecode= <!-- Link to example source code (e.g. [[Media:The Code Example ZIP.zip]]) -->
 +
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 +
|sdk=<!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
 +
|devicecompatability=<!-- Compatible devices (e.g.: All* (must have GPS) ) -->
 +
|signing=<!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
 +
|capabilities=<!-- Capabilities required (e.g. Location, NetworkServices.) -->
 +
|author=[[User:Flycarl]]
 
}}
 
}}
  

Revision as of 09:40, 26 June 2011

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: flycarl (18 Nov 2010)
Last edited: hamishwillee (26 Jun 2011)


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的国际化和本地化的操作。

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessage>
// Use Qt Mobility API namespace
QTM_USE_NAMESPACE
 
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
service = new QMessageService(this);
connect(ui->sendButton,SIGNAL(clicked()),this,SLOT(sendMessage()));
}
 
void MainWindow::sendMessage()
{
QMessage message;
QMessageAddressList toList;
QMessageAddress::Type addrType(QMessageAddress::Phone);
toList.append(QMessageAddress(addrType, ui->numberEdit->text()));//电话号码//这里可以设置群以号码
message.setType(QMessage::Sms);//短信类型
message.setTo(toList);
message.setBody(ui->messageEdit->text()); //设置短信内容为lineEdit中的字符,支持中文
service->send(message);
}
 
MainWindow::~MainWindow()
{
delete ui;
}


QMessageService 调用symbian的API发送短信,这里的构造函数使用AO异步实现,不能把这里的service做成局部变量,否则有可能应为构造没完成,导致调用send方法时出错,报“A data abort exception has occurred”

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
 
#include <QMainWindow>
#include <QMessageService>
 
QTM_USE_NAMESPACE
 
namespace Ui {
class MainWindow;
}
 
class MainWindow : public QMainWindow
{
Q_OBJECT
 
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void sendMessage();
 
private:
Ui::MainWindow *ui;
QMessageService *service;
};
 
#endif // MAINWINDOW_H

下载Demo

File:SendSMS.zip

参考链接

365 page views in the last 30 days.
×