使用我的phonegap jquery应用程序无法在页面加载时获取连接状态

时间:2013-02-04 06:47:32

标签: javascript jquery cordova jquery-mobile

我正在尝试在我的phonegap应用加载时加载数据Feed。它工作正常,除了当我没有数据连接时,我希望系统能够感知到这一点,然后给出一个很好的消息,例如“抱歉,你没有数据连接。”。页面全部加载后,我可以单击一个按钮并运行一个告诉我连接状态的函数。但是,它只是在页面加载时不会自动运行此代码,我无法弄清楚原因。我在iOS或Android上测试时遇到同样的问题。我只删除了基础知识,所以希望有人找不到问题。

<html>
<head>
    <link rel="stylesheet" type="text/css" href="css/main.css" />   
    <script type="text/javascript" charset="utf-8" src="js/jquery-1.7.min.js"></script>
    <script type="text/javascript" charset="utf-8" src="js/main.js"></script>
    <script src="js/jquery.jsonp-2.1.4.min.js"></script>    
<script>
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(states[networkState] == 'No network connection') { show_no_connection(); }
}

$(document).ready(function(){
    checkConnection();
});

function show_no_connection() {
        $('#status').html('You Have No Data Connection!');  
}   


</script>       

</head>
<body>

 <div id="status">SO FAR, YOU HAVE A CONNECTION.</div>
     <a href='javascript:void(0);' onclick='checkConnection();'>Check your connection</a><br/><br/> 
    <script type="text/javascript" src="cordova-2.2.0.js"></script>
    <script type="text/javascript" src="js/index.js"></script>
    <script type="text/javascript">
        app.initialize();
    </script>       
</body>

1 个答案:

答案 0 :(得分:1)

经典 jQuery 语法 $(document).ready(function(){ 不应与 jQuery Mobile 一起使用,它不会正确工作,更不用说它不能用于初始化 Phonegap 代码。有关此主题的更多信息,请参阅此 ARTICLE 。或者找到 HERE

这应该用于解决这个问题:

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

// PhoneGap is loaded and it is now safe to make calls PhoneGap methods
function onDeviceReady() {
    checkConnection();
}

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(states[networkState] == 'No network connection') { show_no_connection();
}

function show_no_connection() {
    $('#status').html('You Have No Data Connection!');  
}   

代码需要等待 Phonegap 框架初始化,它是这样完成的:

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