如何检查Phonegap中的Internet网络连接

时间:2015-01-20 04:51:05

标签: html cordova jquery-mobile

我无法检查phonegap中的互联网连接。 这是我到目前为止所做的。

document.addEventListener(" deviceready",onDeviceReady(),false);

function onDeviceReady() { 

    checkConnection();
}

function checkConnection() {
           var networkState = navigator.connection.type; 
           var states = {};
           states[Connection.UNKNOWN]  = 'Unknown connection';
           states[Connection.ETHERNET] = 'Ethernet connection';
           states[Connection.WIFI]     = 'WiFi connection';
           states[Connection.CELL_2G]  = 'Cell 2G connection';
           states[Connection.CELL_3G]  = 'Cell 3G connection';
           states[Connection.CELL_4G]  = 'Cell 4G connection';
           states[Connection.CELL]     = 'Cell generic connection';
           states[Connection.NONE]     = 'No network connection';

           alert('Connection type: ' + states[networkState]);
       } 

警报对话框未弹出。

2 个答案:

答案 0 :(得分:0)

首先需要安装 Cordova Network Information Plugin

https://git-wip-us.apache.org/repos/asf/cordova-plugin-network-information.git

document.addEventListener("deviceready", init, false);
var Status = "";

function init() {  
   document.addEventListener("offline", offLine, false);
   document.addEventListener("online", onLine, false);
}

function offLine() {  
   if(Status != 'disconnected') {
      Status = 'disconnected';
     // Your code
   }
}

function onLine() { 
   if(Status != 'connected' && Status != '') {
       Status = 'connected';
       // Your code    
   }
}

答案 1 :(得分:0)

Phonegap获取设备连接类型。

1.在你的html页面中包含Phonegap / Cordova javascript文件。

2.添加EventListner以在设备准备好时加载该功能。   document.addEventListener(" deviceready",connectionType,false);

3.创建一个函数来获取设备的网络连接类型。

function connectionType() {
    var  networkConnectionType =  navigator.network.connection.type;
           alert('Connection Type: ' +  networkConnectionType );
}