In one of the previous articles, we described how to use the URL disptacher. In this article, let's take a closer look at how to implement it.
1) Create an app with URL dispatcher we first open our SDK and then create a basic QML template app. We call this application "MYAPP". We first add a file called "Myapp.url-dispatcher" in the root directory of "MYAPP", which reads as follows:
[{"Protocol": "Launchmyapp"}]
Such a definition allows any call that begins with "launchmyapp:///" in qt.openurlexternally () to open the app. Like what:
Qt.openurlexternally ("launchmyapp:///123");
At the same time, we modify our Manifest.json file as follows:
{ "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 "}
Note the "URLs" item here. In this case, we can basically make our application be called by other applications. In order to get the parameters from the calling application, we also modify our desktop file as follows:
[Desktop Entry] Name=myappexec=qmlscene [email protected] main.qml--%uicon=myapp.pngterminal=falsetype=applicationx-ubuntu-touch= True
Pay attention to this "
--%u"This is part of the join. In order to be able to display the URL information in the program, we have made the following changes to the program:
Import QtQuick 2.0import 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.my App "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 whe n 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 (height:units.gu) page {title:i18n.tr ("MyApp") Column {spacing:units.gu (1) Anchors {margins:units.gu (2) Fill:pa Rent} Row {spacing:units.gu (2) Label {Id:mylab El 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; } } } } }}
Note that the "Urihandler" section here, when the app is run, the URL dispatcher is called, and that part of the code is executed. When the application is not running, we get the input parameter values through the parameters of the descendant:
Arguments { id:myarg defaultargument.help: "Expects URL of the media to play." Defaultargument.valuenames: ["URL"] }
The entire code can be found at the following address:
Https://code.launchpad.net/~liu-xiao-guo/debiantrial/myapp
2) Create Call app
This application is actually very simple. We directly create a basic QML template application, while modifying our main.qml as follows:
Import QtQuick 2.0import 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.la Unchmyapp "/* 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 (height:units.gu) page {title:i18n.tr ("Launchmyapp") Column {spacing:units.gu (1) Anchors {Margins:units.gu (2) F Ill:parent} button {objectName: "button" width:parent.width text:i18n.tr ("Launch MyApp") OnClicked: {qt.openurlexternally ("launchmyapp:///123"); }} button {objectName: "button" Width:parent.width t ext:i18n.tr ("Open MyApp") onclicked: {qt.openurlexternally ("Appid://com.ubuntu.develop Er.unknown.myapp/myapp/current-user-version "); } } } }}
Here we use two methods to invoke our "MyApp". The first is through:
Qt.openurlexternally ("launchmyapp:///123");
The benefit of this approach is that it can be transmitted to the parameters we need and resolved so that different responses can be obtained for different parameters.
Another way is through:
Qt.openurlexternally ("Appid://com.ubuntu.developer.unknown.myapp/myapp/current-user-version");
This method cannot parse any parameters, it can start up the application. We can get some information about the application by the following methods:
To run our application:
We press the first button and if "MYAPP" is not running, it will display the following on the left side of the screen. If "MYAPP" is already running, you can see the following right-hand screen:
If we click on the button below, we can see the following screen:
The source code of the whole project can be found at the following address:
BZR BranchLp:~liu-xiao-guo/debiantrial/launchmyapp
Example of using URL dispatcher