Javascript 检查Phonegap应用程序连接
我正在Phonegap中运行一个非本机应用程序,我想知道我何时有连接。在网上搜索时,我找到了一种方法,可以知道我是否在我的应用程序中获得了连接,但我用代码实现了,但没有成功 我是这样发现的:Javascript 检查Phonegap应用程序连接,javascript,android,jquery,cordova,phonegap-plugins,Javascript,Android,Jquery,Cordova,Phonegap Plugins,我正在Phonegap中运行一个非本机应用程序,我想知道我何时有连接。在网上搜索时,我找到了一种方法,可以知道我是否在我的应用程序中获得了连接,但我用代码实现了,但没有成功 我是这样发现的: document.addEventListener("deviceready", onDeviceReady, false); // PhoneGap is loaded and it is now safe to make calls PhoneGap methods
document.addEventListener("deviceready", onDeviceReady, false);
// PhoneGap is loaded and it is now safe to make calls PhoneGap methods
function onDeviceReady() {
check_my_Connection();
}
function check_my_Connection() {
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]);
}
我在脚本的ready
函数中调用函数ondevicerady()
,如下所示:
<script type="text/javascript">
$(document).ready(function(){
/*other code*/
onDeviceReady();
/*other chode*/
});
/*other code functions*/
/*Before the rest of the code, I added the snippet code above of this*/
document.addEventListener("deviceready", onDeviceReady, false);
...
</script>
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicitly call 'app.receivedEvent(...);'
onDeviceReady: function() {
app.receivedEvent('deviceready');
},
// Update DOM on a Received Event
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
console.log('Received Event: ' + id);
}
};
app.initialize();
$(文档).ready(函数(){
/*其他代码*/
ondevicerady();
/*其他选择*/
});
/*其他代码函数*/
/*在剩下的代码之前,我在上面添加了代码片段*/
文件。添加的监听器(“deviceready”,OnDeviceraddy,false);
...
我读到我需要一个cordova.js
,但是(测试版)没有创建它。这项工作需要这个JS文件吗?是否有其他方法可以检测Phonegap应用程序中的连接,而不使用jQueryUI或jQueryMobile?我需要在我的项目的某些文件中做一些更改
我将感谢任何帮助或任何方法来做到这一点
请原谅我的英语。问题必须在你的代码中。只需执行以下操作即可接收连接状态:
cordova create networkInformation com.example.com networkInformation
cd网络信息
cordova平台添加android
cordova插件添加cordova插件网络信息
cordova build
assets
下的platform
->android
文件夹内移动www
文件夹。打开index.js,其外观如下所示:
<script type="text/javascript">
$(document).ready(function(){
/*other code*/
onDeviceReady();
/*other chode*/
});
/*other code functions*/
/*Before the rest of the code, I added the snippet code above of this*/
document.addEventListener("deviceready", onDeviceReady, false);
...
</script>
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicitly call 'app.receivedEvent(...);'
onDeviceReady: function() {
app.receivedEvent('deviceready');
},
// Update DOM on a Received Event
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
console.log('Received Event: ' + id);
}
};
app.initialize();
因此,现在寻找
onDeviceReady: function() {
app.receivedEvent('deviceready');
},
并将其更改为:
onDeviceReady: function() {
app.receivedEvent('deviceready');
checkConnection();
},
还加
function checkConnection() {
var networkState = navigator.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.CELL] = 'Cell generic connection';
states[Connection.NONE] = 'No network connection';
alert('Connection type: ' + states[networkState]);
}
在app.initialize()的正上方代码>
这应该是您的完整index.js。只需启动应用程序,它就会提醒您网络状态:
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicitly call 'app.receivedEvent(...);'
onDeviceReady: function() {
app.receivedEvent('deviceready');
checkConnection();
},
// Update DOM on a Received Event
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
console.log('Received Event: ' + id);
}
};
function checkConnection() {
var networkState = navigator.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.CELL] = 'Cell generic connection';
states[Connection.NONE] = 'No network connection';
alert('Connection type: ' + states[networkState]);
}
app.initialize();
问题必须在代码内部。只需执行以下操作即可接收连接状态:
cordova create networkInformation com.example.com networkInformation
cd网络信息
cordova平台添加android
cordova插件添加cordova插件网络信息
cordova build
assets
下的platform
->android
文件夹内移动www
文件夹。打开index.js,其外观如下所示:
<script type="text/javascript">
$(document).ready(function(){
/*other code*/
onDeviceReady();
/*other chode*/
});
/*other code functions*/
/*Before the rest of the code, I added the snippet code above of this*/
document.addEventListener("deviceready", onDeviceReady, false);
...
</script>
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicitly call 'app.receivedEvent(...);'
onDeviceReady: function() {
app.receivedEvent('deviceready');
},
// Update DOM on a Received Event
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
console.log('Received Event: ' + id);
}
};
app.initialize();
因此,现在寻找
onDeviceReady: function() {
app.receivedEvent('deviceready');
},
并将其更改为:
onDeviceReady: function() {
app.receivedEvent('deviceready');
checkConnection();
},
还加
function checkConnection() {
var networkState = navigator.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.CELL] = 'Cell generic connection';
states[Connection.NONE] = 'No network connection';
alert('Connection type: ' + states[networkState]);
}
在app.initialize()的正上方代码>
这应该是您的完整index.js。只需启动应用程序,它就会提醒您网络状态:
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicitly call 'app.receivedEvent(...);'
onDeviceReady: function() {
app.receivedEvent('deviceready');
checkConnection();
},
// Update DOM on a Received Event
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
console.log('Received Event: ' + id);
}
};
function checkConnection() {
var networkState = navigator.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.CELL] = 'Cell generic connection';
states[Connection.NONE] = 'No network connection';
alert('Connection type: ' + states[networkState]);
}
app.initialize();
我在@BipBip这里回答了一个类似的问题:
document.addEventListener(“offline”,function(){alert(“找不到连接”)},false)代码>你可以把它放在你的html页面的任何地方。这对我来说很有效,试试这个
我在@BipBip这里回答了一个类似的问题:document.addEventListener(“脱机”,function(){alert(“找不到连接”)},false)代码>你可以把它放在你的html页面的任何地方。这对我来说很有效,试试这个
我在@BipBip这里回答了一个类似的问题:document.addEventListener(“脱机”,function(){alert(“找不到连接”)},false)代码>你可以把它放在你的html页面的任何地方。这对我来说很有效,试试这个
我没有使用控制台创建项目,我使用的是Phonegap桌面应用程序,但我知道当我创建应用程序时,所有文件都可以正常工作(我想)。我检查索引js
文件是否与您显示的相同,但最后一行不存在(app.initialize()
位于我的index.html
中。我检查了我的config.xml
,我有一行
。这足以测试我的应用程序中的连接性吗?我没有使用控制台创建项目,我使用的是Phonegap桌面应用程序,但我知道,当我创建应用程序时,这是用所有要运行的文件创建的好吧(我想)。我检查了索引,js
文件,与您显示的相同,但最后一行不存在(app.initialize()
位于我的index.html
中。我检查了我的config.xml
,我有一行
。这足以测试我的应用程序中的连接性吗?我没有使用控制台创建项目,我使用的是Phonegap桌面应用程序,但我知道,当我创建应用程序时,这是用所有要运行的文件创建的好吧(我想)。我检查了索引,js
文件,和你给我看的一样,但是最后一行不存在(app.initialize()
位于我的index.html
中。我检查了我的config.xml
,我有一行
。这样就足以测试我的应用程序的连接性了吗?