[英]Unable to detect no internet connection, in cordova app
I have been searching on how to check internet connection for my cordova app(targeting android at the moment). 我一直在寻找如何检查我的cordova应用程序的互联网连接(目前针对android)。 I have tried the following:
我尝试了以下方法:
$.ajax("rss file url", {
contentType: "application/json; charset=utf-8",
dataType: 'jsonp',
crossDomain: true
})
.done(function (data, textStatus, jqXHR) {
//get rss
})
.fail(function (jqXHR, textStatus, errorThrown) {
//no internet alert
})
.always(function () {
//alert("complete");
});
Also I have tried 我也尝试过
function doesConnectionExist() {
var xhr = new XMLHttpRequest();
var file = "file url";
var randomNum = Math.round(Math.random() * 10000);
xhr.open('HEAD', file + "?rand=" + randomNum, false);
try {
xhr.send();
if (xhr.status >= 200 && xhr.status < 304) {
return true;
} else {
return false;
}
} catch (e) {
return false;
}
}
The problem is that these work absolutely fine in visual studio's ripple emulators, but when I debug on android device I am getting always getting no connection result. 问题是这些在Visual Studio的波纹模拟器中绝对可以正常工作,但是当我在android设备上调试时,却始终无法获得连接结果。 Eventhough, the device is connected to wi-fi.
尽管如此,该设备已连接到Wi-Fi。 My app works fine if I remove this check.
如果我删除此支票,我的应用程序将正常运行。 But, I need to check internet connectivity as my app is basically a rss feed reader.
但是,我需要检查Internet连接,因为我的应用程序基本上是RSS供稿阅读器。 Any help would be appreciated.
任何帮助,将不胜感激。
I have tried the solution mentioned here I have already posted the $.ajax code. 我已经尝试过这里提到的解决方案,我已经发布了$ .ajax代码。 The problem is that the solution works fine on the ripple emulators.
问题在于该解决方案在纹波仿真器上可以正常工作。 But when I release build on an android device from VS15, I keep getting no internet connection results.
但是,当我从VS15在android设备上发布版本时,我始终没有获得任何互联网连接结果。
I would use 我会用
document.addEventListener("online", yourCallbackFunction, false);
Then set some sort of variable to true in your callback function. 然后在回调函数中将某种变量设置为true。
There's a same event for "offline". “离线”有一个相同的事件。
This way you can also handle what happens if the user drops connection while using your app. 这样,您还可以处理如果用户在使用您的应用程序时断开连接会发生的情况。
Source: http://docs.phonegap.com/en/1.8.1/cordova_events_events.md.html#online 来源: http : //docs.phonegap.com/en/1.8.1/cordova_events_events.md.html#online
function doesConnectionExist() {
var xhr = new XMLHttpRequest();
var file = "file url";
var randomNum = Math.round(Math.random() * 10000);
connection=getConnectionStatus();
if(connection){
xhr.open('HEAD', file + "?rand=" + randomNum, false);
try {
xhr.send();
if (xhr.status >= 200 && xhr.status < 304) {
return true;
} else {
return false;
}
} catch (e) {
return false;
}
}
Now inside the getConnectionStatus check for the connection. 现在在getConnectionStatus内部检查连接。
function getConnectionStatus() {
var networkState = navigator.connection.type;
var states = {};
states[Connection.UNKNOWN] = 'unknown';
states[Connection.ETHERNET] = 'ethernet';
states[Connection.WIFI] = 'wifi';
states[Connection.CELL_2G] = '2g';
states[Connection.CELL_3G] = '3g';
states[Connection.CELL_4G] = '4g';
states[Connection.CELL] = 'cellular';
states[Connection.NONE] = 'none';
if(networkState != 'none') {
return true;
} else {
return false;
}
}
the states[] array is for undeerstanding as to what all types of network information u can manipulate with . States []数组用于不确定您可以使用的所有类型的网络信息。
Hope it helps. 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.