標籤:
我們知道對於很多的網路應用來說,網路的串連資訊對於我們來說非常重要。我們有必要對網路的串連資訊進行監測。一旦網路串連斷開,我們需要提醒使用者或做一些處理。在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.0import Ubuntu.Components 1.1import 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
如何在Ubuntu手機中監測網路的串連資訊