如何在QML应用中启动Scope

在这篇文章中,我们将介绍如何在QML应用中调用Scope,并把搜索的关键词传进去。这对有些QML应用需要用到Scope的情况非常有用。更多关于url-dispatcher的知识,请在文章“使用URL dispatcher的范例”看到。


Scope ID


首先我们来讲一下什么是Scope ID。我们打开我们创建的任何一个Scope的manifest.json文件:

{
    "architecture": "@CLICK_ARCH@",
    "description": "A simple Unity scope that accesses the network",
    "framework": "ubuntu-sdk-15.04",
    "hooks": {
        "mytestscope": {
            "apparmor": "mytestscope.apparmor",
            "scope": "mytestscope"
        }
    },
    "maintainer": "XiaoGuo, Liu ",
    "name": "mytestscope.liu-xiao-guo",
    "title": "mytestscope",
    "version": "0.1"
}

这里我们看见了“name”。它的值为“mytestscope.liu-xiao-guo”。那么它的Scope ID为

mytestscope.liu-xiao-guo_mytestscope

这里有一个下划线,“mytestscope”是定义在“hooks”下面的字符串。

那么我们怎么查看在我们手机中的Scope呢?

首先,我们在terminal中进入到手机中:

$adb shell

如何在QML应用中启动Scope_第1张图片

在这里,我们可以看见所有已经安装的click包。假如我们想看上面所显示的“weibo.ubuntu”的包信息,我们可以打入如下的命令:

phablet@ubuntu-phablet:~$ click info weibo.ubuntu

如何在QML应用中启动Scope_第2张图片

这里我们可以看到manifest.json所看到的信息。按照上面的方法,我们可以找到我们相应的Scope ID。

另外,我们也可以通过如下的方法得到应用或者Scope的ID。


如何在QML应用中启动Scope_第3张图片

这里其实已经很清楚地告诉你Scope ID了。


在QML应用中调用Scope


在这里其实已经非常简单了。我们设计一个我们的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: "launchscopes.liu-xiao-guo"

    /*
     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(60)
    height: units.gu(85)

    Page {
        title: i18n.tr("launchscopes")

        Column {
            anchors.centerIn: parent
            spacing: units.gu(2)

            Button {
                text: "Launch Youtube scope"
                onClicked: {
                    console.log("it is clicked!");
                    Qt.openUrlExternally("scope://com.ubuntu.scopes.youtube_youtube");
                }
            }

            Button {
                text: "Launch store scope"
                onClicked: {
                    console.log("it is clicked!");
                    Qt.openUrlExternally("scope://com.canonical.scopes.clickstore?q=weibo");
                }
            }

            Button {
                text: "Launch weibo scope"
                onClicked: {
                    console.log("it is clicked!");
                    Qt.openUrlExternally("scope://weibo.ubuntu_weibo");
                }
            }
        }
    }
}


在这里,我们可以直接调用Scope ID来启动Scope,同时,我们也可以传人参数:

     Qt.openUrlExternally("scope://com.canonical.scopes.clickstore?q=weibo");

如果有空格的话,按照如下的方式:

     scope://com.ubuntu.scopes.youtube_youtube?q=Foo%20Fighters

运行我们的应用:

如何在QML应用中启动Scope_第4张图片   如何在QML应用中启动Scope_第5张图片  


整个项目的源码在: https://github.com/liu-xiao-guo/launchscopes

你可能感兴趣的:(QML,Ubuntu,OS,移动开发,Qt,手机开发)