在上面的一篇文章中,我们介绍了如何使用URL disptacher。在这篇文章中,我们来通过一个范例更进一步来了解如何实现它。
1)创建一个具有URL dispatcher的应用
我们首先打开我们的SDK,然后创建一个最基本的QML template应用。我们把该应用叫做“MyApp”。我们首先在“MyApp”的根目录添加一个文件叫做“MyApp.url-dispatcher”文件,这里面的内容如下:
[
{
"protocol": "launchmyapp"
}
]
这样的定义使得任何在Qt.openUrlExternally()中具有以“launchmyapp:///”开头的调用,就可以打开该应用。比如:
Qt.openUrlExternally("launchmyapp:///123");
同时,我们修改我们的manifest.json文件如下:
{
"architecture": "all",
"description": "description of MyApp",
"framework": "ubuntu-sdk-14.10-dev2",
"hooks": {
"MyApp": {
"apparmor": "MyApp.apparmor",
"desktop": "MyApp.desktop",
"urls": "MyApp.url-dispatcher"
}
},
"maintainer": "XiaoGuo, Liu <xiaoguo.liu@canonical.com>",
"name": "com.ubuntu.developer.unknown.myapp",
"title": "MyApp",
"version": "0.1"
}
[Desktop Entry]
Name=MyApp
Exec=qmlscene $@ main.qml -- %u
Icon=MyApp.png
Terminal=false
Type=Application
X-Ubuntu-Touch=true
注意这里的" -- %u",这是加入的部分。为了能够在程序中显示得到的URL信息,我们对程序做了如下的修改:
import QtQuick 2.0
import Ubuntu.Components 1.1
/*!
\brief MainView with a Label and Button elements.
*/
MainView {
id:root
// 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.unknown.myapp"
Component.onCompleted: {
mylabel.text = "aaaa";
console.log( "arg length: " + myarg.arguments.length );
if ( myarg.defaultArgument === undefined) {
mylabel.text = "undefined";
} else {
mylabel.text = "args: " + myarg.defaultArgument.at(0);
}
console.log("argument: " + myarg.defaultArgument.at(0));
console.log("")
}
Arguments {
id: myarg
defaultArgument.help: "Expects URL of the media to play."
defaultArgument.valueNames: ["URL"]
}
/*
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("MyApp")
Column {
spacing: units.gu(1)
anchors {
margins: units.gu(2)
fill: parent
}
Row {
spacing: units.gu(2)
Label {
id: mylabel
objectName: "label"
text: i18n.tr("Received parameters: ")
}
Label {
id: label
objectName: "label"
text: i18n.tr("")
}
Connections {
target: UriHandler
onOpened: {
// root.applicationName = "good"
mylabel.text = "dddddd";
var para = "";
for (var i = 0; i < uris.length; ++i) {
// application.parseArgument(uris[i])
console.log( uris[i] );
para += uris[i];
}
label.text = para;
}
}
}
}
}
}
注意这里的“UriHandler”部分,当应用在运行时,url dispatcher被调用时,该部分代码会被执行。当应用没有运行时,我们通过传人的参数从而得到输入的参数值:
Arguments {
id: myarg
defaultArgument.help: "Expects URL of the media to play."
defaultArgument.valueNames: ["URL"]
}
整个的代码在如下的地址可以找到:
https://code.launchpad.net/~liu-xiao-guo/debiantrial/myapp
2)创建调用应用
这个应用其实很简单。我们直接创建一个基本的QML template应用,同时修改我们的main.qml如下:
import QtQuick 2.0
import Ubuntu.Components 1.1
/*!
\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.unknown.launchmyapp"
/*
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(100)
height: units.gu(75)
Page {
title: i18n.tr("LaunchMyApp")
Column {
spacing: units.gu(1)
anchors {
margins: units.gu(2)
fill: parent
}
Button {
objectName: "button"
width: parent.width
text: i18n.tr("Launch MyApp")
onClicked: {
Qt.openUrlExternally("launchmyapp:///123");
}
}
Button {
objectName: "button"
width: parent.width
text: i18n.tr("Open MyApp")
onClicked: {
Qt.openUrlExternally("appid://com.ubuntu.developer.unknown.myapp/MyApp/current-user-version");
}
}
}
}
}
这里我们使用了两种方法来调用我们的“MyApp”。第一种是通过:
Qt.openUrlExternally("launchmyapp:///123");
这种方法的好处是可以传人我们需要的参数,并解析,从而对于不同的参数可以得到不同的响应。
另外一种方式是通过:
Qt.openUrlExternally("appid://com.ubuntu.developer.unknown.myapp/MyApp/current-user-version");
运行我们的应用:
我们按下第一个按钮,如果“MyApp”没有运行时,会显示如下的在左边的画面。如果“MyApp”在已经运行的情况下,可以看到如下的右边的画面:
如果,我们点击应用下面的按钮的话,可以看到如下的画面:
整个项目的源码在如下的地址可以找到:
bzr branch
lp:~liu-xiao-guo/debiantrial/launchmyapp