使用URL dispatcher的范例
在上面的一篇文章中,我们介绍了如何使用URL disptacher。在这篇文章中,我们来通过一个范例更进一步来了解如何实现它。
1)创建一个具有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 <[email protected]>", "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"] }
整个的代码在如下的地址可以找到:
2)创建调用应用
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");
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。