检查Phonegap应用程序连接
Check Phonegap app connection
我正在Phonegap中做一个非本地应用程序,我想知道我什么时候有连接。在WEB上搜索,我找到了一种方法来知道我是否在应用程序中获得了连接,但我在代码中实现了,但没有成功。
我的发现是这样的:
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
函数中调用函数onDeviceReady()
,如下所示:
<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>
我读到我需要一个cordova.js
,但PhoneGap桌面应用程序(测试版(没有创建它。这个JS文件是完成这项工作所必需的吗?是否存在另一种方法来检测Phonegap应用程序中的连接,而不使用jQueryUI或jQueryMobile?我需要对我的项目的某些文件进行一些更改?
我将感谢任何帮助或任何方式来做这件事。
第页。对不起,我的英语不好。
问题一定在代码内部。只需执行以下操作即可接收连接状态:
- 打开您的终端/控制台
cordova create networkInformation com.example.com networkInformation
cd networkInformation
cordova platform add android
cordova plugin add cordova-plugin-network-information
cordova build
完成此过程后,您将在桌面上打开创建的文件夹。移动到platform
内的www
文件夹内->assets
下的android
文件夹内。打开你的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');
},
// 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 android应用程序中连接数据库
- 将服务器托管的Web应用程序连接到本地Web套接字
- 如何通过子进程连接Java应用程序后在node.js中编写文件
- 通过SSL/TLS连接访问Vagrant上的Node.js应用程序
- 如何将我的Ionic应用程序与我的java服务器(Spring MVC)连接
- 猫鼬连接在 Express.js 应用程序中自动共享
- 如何将相同的MySQL连接用于我的整个Node.js应用程序
- 从我的web应用程序连接到本地sqlexpress数据库
- 正确连接主干.将Marinette布局视图连接到应用程序
- 将HTML5 PhoneGap应用程序连接到在线数据库
- Meteor测试/部署iOS应用程序以连接真实服务器(而非本地主机)
- android混合应用程序与数据库的离线连接
- 无法从我的 Angular2 应用程序中连接到我的 WebSocket
- 英特尔 XDK 将 mySQL 数据库连接到应用程序
- 将我的整个应用程序连接成一个 HTML、JS 和 CSS 文件
- 防止与我的 Web 应用程序建立多个连接
- 突然无法连接到科尔多瓦应用程序中的网络服务
- 基于英特尔 xdk html5 的应用程序只能在启用互联网连接时工作
- 科尔多瓦 IOS 8.1 打开应用程序时如何检查网络连接
- 连接和缩小 Node.js 应用程序