×
Namespaces

Variants
Actions
(Difference between revisions)

使用Qt Mobility Messaging API发送短信

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Hamishwillee - Set to use Chinese version of articlemetadata)
hamishwillee (Talk | contribs)
m (Hamishwillee - Remove broken external link)
Line 1: Line 1:
[[Category:Code Examples]][[Category:S60]][[Category:Lang-Chinese]][[Category:Qt]][[Category:Qt Mobility]][[Category:Messaging]]
+
[[Category:Code Examples]][[Category:S60]][[Category:Lang-Chinese]][[Category:Qt Mobility]][[Category:Messaging]]
 
{{ArticleMetaData
 
{{ArticleMetaData
 
|language=Lang-Chinese
 
|language=Lang-Chinese
 
|platform=S60 3rd Edition, FP1, FP2<br>S60 5th Edition Symbian^3
 
|platform=S60 3rd Edition, FP1, FP2<br>S60 5th Edition Symbian^3
 
|devices= Nokia 5800 and N8.
 
|devices= Nokia 5800 and N8.
|creationdate=Nov 18, 2010
+
|creationdate=20101118
 
|keywords= QMessageService, QMessage, Chinese
 
|keywords= QMessageService, QMessage, Chinese
 
|sourcecode=[[Media:SendSMS.zip]]
 
|sourcecode=[[Media:SendSMS.zip]]
Line 11: Line 11:
 
|devicecompatability=<!-- Compatible devices (e.g.: All* (must have GPS) ) -->
 
|devicecompatability=<!-- Compatible devices (e.g.: All* (must have GPS) ) -->
 
|signing=<!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
 
|signing=<!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
|capabilities=<!-- Capabilities required (e.g. Location, NetworkServices.) -->
+
|capabilities=NetworkServices, LocalServices, ReadUserData, WriteUserData, UserEnvironment, ReadDeviceData, WriteDeviceData
 
|author=[[User:Flycarl]]
 
|author=[[User:Flycarl]]
 
}}
 
}}
Line 20: Line 20:
 
这里的代码演示如何用Qt Mobility的Messaging API发送短信,自Qt Mobility 1.1.0可以发送中文短信。
 
这里的代码演示如何用Qt Mobility的Messaging API发送短信,自Qt Mobility 1.1.0可以发送中文短信。
 
代码需要以下能力 [[NetworkServices]] [[LocalServices]] [[ReadUserData]] [[WriteUserData]] [[UserEnvironment]] [[ReadDeviceData]] [[WriteDeviceData]] 。自签名不够用,至少需要 [[Developer certificate | 开发者签名]]  ,也可以用 [[Open Signed Online]]来测试。
 
代码需要以下能力 [[NetworkServices]] [[LocalServices]] [[ReadUserData]] [[WriteUserData]] [[UserEnvironment]] [[ReadDeviceData]] [[WriteDeviceData]] 。自签名不够用,至少需要 [[Developer certificate | 开发者签名]]  ,也可以用 [[Open Signed Online]]来测试。
 
  
 
==准备条件==
 
==准备条件==
Line 123: Line 122:
 
==下载Demo==
 
==下载Demo==
 
[[File:SendSMS.zip]]
 
[[File:SendSMS.zip]]
==参考链接==
 
*[http://www.devdiv.net/index.html 用QMessageService发送中文短信的探讨了解QT Mobility的设计]
 

Revision as of 09:13, 19 October 2011

Article Metadata

代码示例
测试基于
设备:: Nokia 5800 and N8.

兼容于
平台: S60 3rd Edition, FP1, FP2
S60 5th Edition Symbian^3
Symbian

平台安全性
能力: NetworkServices, LocalServices, ReadUserData, WriteUserData, UserEnvironment, ReadDeviceData, WriteDeviceData

文章
关键词: QMessageService, QMessage, Chinese
flycarl 在 18 Nov 2010 创建
最后由 hamishwillee 在 19 Oct 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

377 page views in the last 30 days.
×