如何定期检查Phonegap的互联网连接? [英] How to check internet connection in Phonegap periodically?

查看:126
本文介绍了如何定期检查Phonegap的互联网连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Phonegap。我需要定期检查网络连接。实际上,我从服务器获取一些数据。如果没有连接,我需要显示错误警报。



我google了搜索并找到了解决方案。但它不是确定。因为我需要定期检查连接。

 < html> 
< head>
< title> navigator.network.connection.type示例< / title>
< script type =text / javascriptcharset =utf-8src =phonegap-1.0.0.js>< / script>
< script type =text / javascriptcharset =utf-8>
//等待PhoneGap加载
document.addEventListener(deviceready,onDeviceReady,false);
function onDeviceReady(){
checkConnection();
}

function checkConnection(){
var networkState = navigator.network.connection.type;
var states = {};
状态[Connection.UNKNOWN] ='未知连接';
states [Connection.ETHERNET] ='以太网连接';
状态[Connection.WIFI] ='WiFi连接';
states [Connection.CELL_2G] ='手机2G连接';
states [Connection.CELL_3G] ='手机3G连接';
states [Connection.CELL_4G] ='手机4G连接';
状态[Connection.NONE] ='无网络连接';
alert('Connection type:'+ states [networkState]);
}
< / script>
< / head>
< body>
< p>一个对话框将报告网络状态。< / p>
< / body>
< / html>

它仅在应用程序启动时第一次检查。但我需要定期检查,因为我做套接字编程。如果互联网有问题,我需要显示它。

解决方案

工作范例: http://jsfiddle.net/Gajotres/d5XYR/



每隔预定时间使用间隔定时器检查互联网连接。这个解决方案需要HTML5浏览器,但这不是一个问题,因为jQuery Mobile已经需要HTML5浏览器。在这种情况下,计时器将每100毫秒检查互联网连接,并将最终结果设置为一个javascript全局变量。



一切都取决于这一行:

  window.navigator.onLine  - 如果用户离线,它将为​​false。 

最终解决方案:

  var connectionStatus = false; 

$(document).on('pagebeforeshow','#index',function(){
setInterval(function(){
connectionStatus = navigator.onLine?'online ':'offline';
},100);
$(document).on('click','#check-connection',function(){
alert(connectionStatus);
});
});

测试日期:


$ b b

  • Windows Firefox


  • Windows Google Chrome


  • Windows IE9和IE10


  • Android 4.1.1 Chrome

    iPad 3 Safari


  • iPad3 Chrome



I am working with Phonegap. I need to check the network connection periodically. Actually I am getting some data from a server. If there is no connection, I need to show an error alert.

I googled it and found the solution. But it is not ok. Because I need to check the connection periodically.

<html>
  <head>
    <title>navigator.network.connection.type Example</title>
    <script type="text/javascript" charset="utf-8" src="phonegap-1.0.0.js"></script>
    <script type="text/javascript" charset="utf-8">
    // Wait for PhoneGap to load
    document.addEventListener("deviceready", onDeviceReady, false);
    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]);
    }
    </script>
  </head>
  <body>
    <p>A dialog box will report the network state.</p>
  </body>
</html>

It check only the first time when the application launches. But I need to check it periodically, because I am doing socket programming. If there is a problem with internet, I need to show it. But this code only shows at launch time.

解决方案

Working example: http://jsfiddle.net/Gajotres/d5XYR/

Use interval timer to check internet connection every predefined time. This solution requires HTML5 browser but this is not a problem because jQuery Mobile already requires HTML5 browser. In this case timer will check internet connection every 100 ms and set final result into a javascript global variable.

Everything depends on this line:

window.navigator.onLine -- it will be false if the user is offline.

Final solution:

var connectionStatus = false;

$(document).on('pagebeforeshow', '#index', function () {
    setInterval(function () {
        connectionStatus = navigator.onLine ? 'online' : 'offline';
    }, 100);
    $(document).on('click', '#check-connection', function () {
        alert(connectionStatus);
    });
});

Tested on:

  • Windows Firefox

  • Windows Google Chrome

  • Windows IE9 and IE10

  • Android 4.1.1 Chrome

  • iPad 3 Safari

  • iPad3 Chrome

这篇关于如何定期检查Phonegap的互联网连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆