如何在Ubuntu手机中监测网络的连接信息

我们知道对于很多的网路应用来说,网路的连接信息对于我们来说非常重要。我们有必要对网路的连接信息进行监测。一旦网路连接断开,我们需要提醒用户或做一些处理。在Ubuntu平台上,我们可以使用connectivity库来查看。


我们可以利用SDK的模版来创建一个最简单的QML应用。因为我们要使用connectivity,所以我们必须加入“connectivity”的security policy。


技术分享


在我们的开发者网站上虽然也有NetworkStatus的介绍,但是可能并不是很全,我们可以使用如下的命令来得到更多的信息:


$qmlplugindump Ubuntu.Connectivity 1.0


显示的结果如下:


import QtQuick.tooling 1.1

// This file describes the plugin-supplied types contained in the library.
// It is used for QML tooling purposes only.
//
// This file was auto-generated by:
// 'qmlplugindump Ubuntu.Connectivity 1.0'

Module {
    Component {
        name: "NetworkingStatus"
        prototype: "ubuntu::connectivity::NetworkingStatus"
        exports: ["NetworkingStatus 1.0"]
        isCreatable: false
        isSingleton: true
        exportMetaObjectRevisions: [0]
        Property { name: "online"; type: "bool"; isReadonly: true }
        Property { name: "limitedBandwith"; type: "bool"; isReadonly: true }
        Signal {
            name: "onlineChanged"
            Parameter { name: "value"; type: "bool" }
        }
        Signal {
            name: "limitedBandwithChanged"
            Parameter { name: "value"; type: "bool" }
        }
    }
    Component {
        name: "ubuntu::connectivity::NetworkingStatus"
        prototype: "QObject"
        Enum {
            name: "Limitations"
            values: {
                "Bandwith": 0
            }
        }
        Enum {
            name: "Status"
            values: {
                "Offline": 0,
                "Connecting": 1,
                "Online": 2
            }
        }
        Property { name: "limitations"; type: "QVector<Limitations>"; isReadonly: true }
        Property { name: "status"; type: "Status"; isReadonly: true }
        Signal {
            name: "statusChanged"
            Parameter { name: "value"; type: "Status" }
        }
    }
}

这里我们可以看到有一个叫做“status”的属性。我们可以通过监测这个属性的变化而得到网路的连接状态的变化。我们的main.qml的文件如下:


import QtQuick 2.0
import Ubuntu.Components 1.1
import Ubuntu.Connectivity 1.0

/*!
    \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: "networkstatus.ubuntu"

    /*
     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)

    property real margins: units.gu(2)
    property real buttonWidth: units.gu(9)

    Connections {
        target: NetworkingStatus
        // full status can be retrieved from the base C++ class
        // status property
        onStatusChanged: {
            console.log("name: " + value );

            if (value === NetworkingStatus.Offline)
                console.log("Status: Offline")
            if (value === NetworkingStatus.Connecting)
                console.log("Status: Connecting")
            if (value === NetworkingStatus.Online)
                console.log("Status: Online")
        }
    }

    Page {
        title: i18n.tr("Networking Status")

        Column {
            anchors.centerIn: parent
            Label {
                // use the online property
                text: NetworkingStatus.online ? "Online" : "Not online"
                fontSize: "large"
            }
            Label {
                // use the limitedBandwith property
                text: NetworkingStatus.limitedBandwith ? "Bandwith limited" : "Bandwith not limited"
                fontSize: "large"
            }
        }
    }
}

最终运行我们的应用,我们可以看到在wifi断开和连接上时的状态变化:


技术分享  技术分享


测试代码在: git clone https://gitcafe.com/ubuntu/networkstatus.git




郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。