We know that for many Web applications, the network connection information is very important to us. It is necessary for us to monitor the connection information of the network. Once the network connection is disconnected, we need to alert the user or do some processing. On the Ubuntu platform, we can use the connectivity library to view.
We can use the template of the SDK to create one of the simplest QML applications. Because we want to use connectivity, we have to join the security policy of "connectivity".
Although there are also networkstatus on our developer's website, it may not be very full, we can use the following command to get more information:
$qmlplugindump Ubuntu.connectivity 1.0
The results shown are as follows:
Import qtquick.tooling 1.1//This file describes the Plugin-supplied types contained in the library.//It's used for QML Tooling purposes only.////This file is auto-generated by://' qmlplugindump ubuntu.connectivity 1.0 ' Module {Component {Name: "Networkingstatus" prototype: "Ubuntu::connectivity::networkingstatus" exports: ["Networking Status 1.0 "] Iscreatable:false issingleton:true exportmetaobjectrevisions: [0] Property {NA Me: "Online"; Type: "BOOL"; Isreadonly:true} Property {name: "Limitedbandwith"; Type: "bool"; Isreadonly:true} Signal { Name: "Onlinechanged" Parameter {name: "value"; Type: "bool"}} Signal {name: "Limi Tedbandwithchanged "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}} Prope Rty {name: "Limitations"; Type: "Qvector<limitations>"; Isreadonly:true} property {name: "status"; Type: "Status"; Isreadonly:true} Signal {name: "statuschanged" Parameter {name: "value"; Type: "Status"} } }}
Here we can see that there is a property called "status". We can get the change of the connection state of the network by monitoring the change of this property. Our main.qml files are as follows:
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"/* The 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) property real Margins:units.gu (2) proper Ty Real ButtonWidth:units.gu (9) Connections {target:networkingstatus//full status can retrieved F Rom 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:p Arent Label {//Use of the online property text:NetworkingStatus.online? ' Online ': ' Not online ' fontSize: ' large '} Label {//Use the Limited Bandwith Property Text:NetworkingStatus.limitedBandwith? "Bandwith Limited": "Bandwith not Limited" fontSize: "Large"}}}
Finally running our app, we can see the change in status when WiFi is disconnected and connected:
Test code in: Git clone https://gitcafe.com/ubuntu/networkstatus.git
How to monitor network connection information in Ubuntu phone