1
function checkConnection() {
                            var networkState = navigator.network.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.NONE]     = 'No network connection';


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

                }

在这里,如果只有互联网连接不可用,我想显示警报?我正在使用 if 语句if(statusValue == 'none'){ alert }但这里在我的移动应用程序中不起作用。这里我使用的是Phonegap-1.3.0.js

4

5 回答 5

4

做就是了

networkState = navigator.network.connection.type; 
if (networkState == Connection.NONE)
{
  alert('No internet connection ');
};
于 2012-04-10T05:20:23.267 回答
2

尝试这个:

function checkConnection() {
    network = navigator.network.connection.type;


    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.NONE] = 'No network connection';
    // alert('Connection type: ' + states[network]);
    return states[network];
}
     if (states[network] == 'No network connection')
{
    alert('Connection type: ' + states[network]);
}
于 2013-10-04T05:39:03.163 回答
1

试试下面的代码。

var networkState = navigator.network.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.NONE]     = 'No network connection';

if ((states[networkState]) == states[Connection.NONE])
{
alert("Please check your internet connectivity and try again"); 
}
于 2014-07-16T06:49:39.940 回答
1

您还可以使用 navigator.network.isReachable 以几乎 100% 的正常运行时间来 ping 一些网站,例如 google。在所有移动操作系统或某些特定版本的 Android 中它不是都可以工作吗?

于 2012-04-10T04:57:05.753 回答
1

这应该可以帮助您:

// Wait for Cordova to load
document.addEventListener("deviceready", onDeviceReady, false);
// Cordova is ready
function onDeviceReady() {
document.addEventListener("offline", whenOffline, false);
return false;
}

function whenOffline() {
navigator.notification.alert(
'Sorry your internet connection is not working, please enable it !', // message
alertDismissed, // callback
'Settings', // title
'Done' // buttonName
);
return false;
}
于 2013-08-01T19:33:48.633 回答