检查PhoneGap应用程序中的网络连接

时间:2014-04-11 06:54:00

标签: android cordova

我在很长一段时间内遇到这个问题,我正在检查网络连接如下

function CheckConnection()
{ if(navigator.network.connection.type == Connection.NONE){
                alert("Please connect to Internet");

            }else{

             loadingStart();
    setTimeout(function () {
        loadingEnd();
        $.mobile.changePage('#page8');
    }, 5000);
    return false; 



   function loadingStart() {
    $.mobile.loading('show', {
        text: "Please Wait...",
        textVisible: true,
        theme: 'b',

    });
}

function loadingEnd() {
    $.mobile.loading("hide");
        }  

    }
}

当我点击一个按钮时检查这个连接,当没有连接时我得到警告声明,但当我在应用程序的中间并且如果数据连接打开然后单击按钮它仍然显示警告声明itz不会进入其他部分。

1 个答案:

答案 0 :(得分:0)

在加载应用后尝试使用onlineoffline事件:

document.addEventListener('online',app.online, false);
document.addEventListener('offline',app.offline, false);

ONLOAD:

    var networkState = navigator.connection.type;

    var states = {};
    states[Connection.UNKNOWN]  = false;
    states[Connection.ETHERNET] = true;
    states[Connection.WIFI]     = true;
    states[Connection.CELL_2G]  = true;
    states[Connection.CELL_3G]  = true;
    states[Connection.CELL_4G]  = true;
    states[Connection.CELL]     = true;
    states[Connection.NONE]     = false;
    global.connectionStatus = states[networkState];
    app.connectionStatus(global.connectionStatus);
    if(global.connectionStatus) {
        //connected
    }
    else{
        //not connected
    }

初步检查后:

online: function() {
    //regained connection
},
offline: function() {
    //lost connection
}