由于一些平台安全性的原因,Ubuntu手机目前暂时没有提供供第三方开发者发送短信及拨打电话的接口,但是在实际的应用中,我们也许会需要用到发送短信息或拨打电话。这个时候我们怎么办呢?我们在前面的文章“使用URL dispatcher的范例”中已经介绍了如何使用url dispatcher来调用第三方应用的方法。这里我们用该方法来展示如何在我们的应用中发送短信息及拨打电话。
首先,我们创建一个最简单的“App with Simple UI”模版应用,并修改我们的“main.qml”文件如下:
import QtQuick 2.0
import Ubuntu.Components 1.1
import Ubuntu.Components.ListItems 0.1 as ListItem
/*!
\brief MainView with a Label and Button elements.
*/
MainView {
// objectName for functional testing purposes (autopilot-qt5)
objectName: "mainView"
// Note! applicationName needs to match the "name" field of the click manifest
applicationName: "com.ubuntu.developer.liu-xiao-guo.phone"
/*
This property enables the application to change orientation
when the device is rotated. The default is false.
*/
//automaticOrientation: true
// Removes the old toolbar and enables new features of the new header.
useDeprecatedToolbar: false
width: units.gu(50)
height: units.gu(75)
Page {
title: i18n.tr("Phone")
function call(phonenumber) {
Qt.openUrlExternally("tel:///" + encodeURIComponent(phonenumber))
}
function sendMessage(phonenumber, text) {
Qt.openUrlExternally("message:///" + encodeURIComponent(phonenumber))
}
Column {
spacing: units.gu(1)
anchors {
margins: units.gu(2)
fill: parent
}
Row {
anchors.horizontalCenter: parent.horizontalCenter
Label {
anchors.verticalCenter: parent.verticalCenter
text: i18n.tr("Number: ")
}
TextField {
id: inputnumber
placeholderText: "please type your phone number"
text: "1111111111"
}
}
Button {
width: parent.width
text: i18n.tr("Call")
onClicked: {
call(inputnumber.text)
}
}
ListItem.Divider {}
Row {
anchors.horizontalCenter: parent.horizontalCenter
Label {
anchors.verticalCenter: parent.verticalCenter
text: i18n.tr("Number: ")
}
TextField {
id: inputnumber1
placeholderText: "please type your phone number"
text: "22222222222"
}
}
TextEdit {
id: messageText
}
Button {
width: parent.width
text: i18n.tr("Send Message")
onClicked: {
sendMessage(inputnumber1.text)
}
}
}
}
}
这个应用的设计非常简单。我们的UI如下:
我们在上面的号码输入框中输入自己想要拨打或发送短信的号码,按下“Call”或“Send Message”按钮,就可以拨打电话或发送短信了。只不过短信或电话的应用被调用起来来完成这个动作。从安全的角度来说,这个需要用户的交互才可以完成。对手机是非常安全的。我们使用了如下的代码来完成url dispatcher的工作:
function call(phonenumber) {
Qt.openUrlExternally("tel:///" + encodeURIComponent(phonenumber))
}
function sendMessage(phonenumber, text) {
Qt.openUrlExternally("message:///" + encodeURIComponent(phonenumber))
}
整个应用的完整代码在如下的地址可以找到:
bzr branch lp:~liu-xiao-guo/debiantrial/phone