我正在使用 cordova 3.5.0 开发我的 phonegap 应用程序。在那我想在网络服务调用之前检查互联网连接。所以我使用命令 cordova plugin add org.apache.cordova.network-information 添加了网络状态插件。插件成功安装在我的应用程序中。
添加插件后,我添加了 2 个 EventListener,一个用于在线,另一个用于离线。
var app = {
// Application Constructor
initialize: function() {
console.log('App initializing...');
this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
//This is to allow testing in desktop browser where there is no device ready event
if (navigator.userAgent.match(/(iPhone|iPod|iPad|Android|BlackBerry)/)) {
document.addEventListener('deviceready', this.onDeviceReady, false);
document.addEventListener("offline", onOffline, false);
document.addEventListener("online", onOnline, false);
} else {
this.onDeviceReady();
}
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicity call 'app.receivedEvent(...);'
onDeviceReady: function() {
StatusBar.overlaysWebView(false);
app.receivedEvent('deviceready');
},
onOnline:function(){
console.log("Online");
}.
onOffline: function(){
console.log("Offline");
},
// Update DOM on a Received Event
receivedEvent: function(id) {
require(['router'], function(Router){
Router.getInstance();
console.log('Backbone callback...');
});
}
};
所以我使用了另一种方法来检查 Phonegap 文档中提到的在线状态
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]);
}
始终返回连接到互联网的模式,例如“WiFi 连接”。虽然我改变了我断开的互联网连接。
帮我解决这个问题。
Best Answer-推荐答案 strong>
我遇到了同样的问题nline/offline 事件没有触发,有时当它离线时,应用程序崩溃了......
解决方案:
- 适当的权限标签必须紧跟在标签之后,在 AndroidManifest.xml 文件中......它可能看起来很荒谬,但否则事件不会触发。
最后,您的文件将如下所示:
<manifest android:hardwareAccelerated="true" android:versionCode="10000" android:versionName="1.0.0" android:windowSoftInputMode="adjustPan" package="br.com.burkard.app" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
关于ios - 在 cordova 3.5.0 中未调用离线和在线事件,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/24935014/
|