简体   繁体   English

如何检查设备是在线还是离线Phonegap

[英]How to check device is Online or Offline Phonegap

What is the easiest way to check whether the device(smartphone) is online or offline. 检查设备(智能手机)是在线还是离线的最简单方法是什么。 I'm working with phonegap, jquery mobile. 我正在使用phonegap,jquery mobile。 I found this one. 我找到了这个。

document.addEventListener("online", yourCallbackFunction, false);

What I want to do is to check the Internet connection and decide about to get the data from the internet or from the device local database. 我想要做的是检查互联网连接,并决定从互联网或设备本地数据库获取数据。

if(deviceoffline)
  getDataFromLokalDatabase();
else
  getDataFromInternet();

This is the code using native phonegap code: 这是使用本机phonegap代码的代码:

function checkInternet() {

    var networkState = navigator.connection.type;

    if(networkState == Connection.NONE) {

        onConnexionError();
        return false;

    } else {

       return true;
    }
}

You can use navigator.onLine : 您可以使用navigator.onLine

var isOffline = 'onLine' in navigator && !navigator.onLine;

if ( isOffline ) {
    //local db
}
else {
    // internet data
}

But be aware of that if navigator.onLine isn't supported isOffline will always be false 但请注意,如果不支持navigator.onLineisOffline将始终为false

Using Jquery make a ajax call and check the error code, If you get error code as 0 the device is offline 使用Jquery进行ajax调用并检查错误代码,如果错误代码为0则设备处于脱机状态

$.ajax({
    //your ajax options
    error: function (request) { 
         if (request.status == 0) {
            alert("you're offline");
        }
    }
});

Beware, while navigator.onLine is the simplest solution, it does not behave consistently on all browsers . 请注意,虽然navigator.onLine是最简单的解决方案, 但它在所有浏览器上的表现并不一致 On Chrome it will tell you that it is online if you have a LAN cable in, even if you have no internet. 在Chrome上,即使您没有互联网,如果您有LAN电缆,它也会告诉您它在线。

You can use the cordova plugin network-information 您可以使用cordova插件网络信息

You can also try to make an AJAX request to your server; 您还可以尝试向服务器发出AJAX请求; usually you want to know if you are offline because in that case you cannot communicate with the server , so "offline" often means "not able to communicate with the server" (which can be also caused by your server being offline). 通常你想知道你是否离线,因为在这种情况下你无法与服务器通信 ,因此“离线”通常意味着“无法与服务器通信”(这也可能是由于服务器脱机造成的)。 Playing with timeouts or several requests is also useful if you need to check bandwidth or link quality. 如果您需要检查带宽或链接质量,则使用超时或多个请求也很有用。

Offline does not mean the same for every use case, you first need to know which of the above techniques is best suited for you, then implement one or several of them. 对于每个用例,离线并不意味着相同,您首先需要知道上述哪种技术最适合您,然后实现其中的一个或几个。

What I want to do is to check the Internet connection and decide about to get the data from the internet or from the device local database. 我想要做的是检查互联网连接,并决定从互联网或设备本地数据库获取数据。

It seems that checking the link with your server through a request is the best option for you. 通过请求检查与服务器的链接似乎是最佳选择。

the native onLine is somewhat shaky, especially in an web app environment.. but can be used for an initial check upon pageLoad, however it is somewhat useless then as if you get to that point you have an internet connection. 本机onLine有点不稳定,特别是在Web应用程序环境中..但可以用于pageLoad的初始检查,但它有点无用,就好像你到达那一点,你有一个互联网连接。

attempting to ajax something is far more reliable. 尝试ajax更可靠。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM