我刚接触电话差距,并使用命令行界面(CLI)构建了一个Android应用程序但是当该功能加载以提醒我,如果手机在线或离线,它将失败。以下是我如何安装模块的代码和细节。
我的问题是我得到的最后一个警告是“OK58”然后手机只是继续加载和加载。
Javascript代码
function onDeviceReady() {
"use strict";
alert("ok55");
document.addEventListener("online", onOnline, false);
document.addEventListener("offline", onOffline, false);
alert("ok58");
//try{
var networkState = navigator.connection && navigator.connection.type;
// setTimeout(function(){ networkState = navigator.connection&& 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.NONE] = 'No network connection';
alert('Connection type: ' + states[networkState]);
//}, 500);
// }catch(e){
// alert(e);
// $.each(navigator, function(key, value){
// alert(key+' => '+value);
// });
// }
alert("hello");
我的html标题。上面的代码包含在common.js
中<head>
<meta charset="utf-8" />
<meta name="format-detection" content="telephone=no" />
<meta name="msapplication-tap-highlight" content="no" />
<meta http-equiv="Content-Security-Policy" content="default-src *; style-src * 'unsafe-inline'; script-src * 'unsafe-inline'; media-src *">
<!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Eurotech</title>
<link rel="stylesheet" href="css/style_signiture.css"/>
<link rel="stylesheet" href="css/jquery.mobile-1.1.0.min.css"/>
<script src="cordova.js" type="text/javascript"></script>
<script src="js/phonegap-nfc-0.3.0.js"></script>
<script src="js/nfc.js"></script>
<script src="js/common.js"></script>
<script src="js/jquery-1.7.1.min.js"></script>
<script src="js/jquery.ajaxmanager.js"></script>
<script src="js/jqm.page.params.js"></script>
<script src="js/jquery.mobile-1.1.0.min.js"></script>
<script src="js/task.js"></script>
<script src="js/modernizr.custom.34982.js"></script>
<script src="js/application.js"></script>
<script src="js/signatureCapture.js"></script>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
</head>
在我的config.xml文件中,我有以下内容
<plugin name="cordova-plugin-whitelist" version="1" />
<plugin name="org.apache.cordova.network-information" spec="0.2.15" source="pgb" />
<plugin name="NfcPlugin" value="com.chariotsolutions.nfc.plugin.NfcPlugin"/>
和
<gap:plugin name="cordova-plugin-dialogs" source="npm" />
<gap:plugin name="org.apache.cordova.network-information" version="0.2.15" />
我使用
通过命令行安装了插件phonegap plugin add org.apache.cordova.dialogs
phonegap plugin add cordova-plugin-network-information
我认为这就是一切。我怎样才能更好地调试这个?任何想法都错了吗?
答案 0 :(得分:2)
你的代码中有一些拼写错误,这应该有效(未经测试):
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.NONE] = 'No network connection';
alert('Connection type: ' + states[networkState]);
所有设备请求都是事件驱动的。因此将它们置于暂停状态是没有意义的。
请阅读https://www.npmjs.com/package/cordova-plugin-network-information上的文档。
对于调试安装https://www.npmjs.com/package/cordova-plugin-console,您可以在SDK和/或浏览器开发人员信息中查看输出。使用Safari for iOS和Chrome for Android。
如果你有一个旧的Android,你也可以使用weinre进行调试:https://people.apache.org/~pmuellr/weinre-docs/latest/
<强>更新强>
这里有一个工作示例,在iPad 9.2上进行了测试。
var app = {
initialize: function () {
document.addEventListener("deviceready", app.onDeviceReady, false);
document.addEventListener("online", app.onOnline, false);
document.addEventListener("offline", app.onOffline, false);
}, onDeviceReady: function () {
alert("App is started");
app.getNetworkState();
}, getNetworkState: function () {
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.NONE] = "No network connection";
alert("Connection type: " + states[networkState]);
}, onOnline: function(){
alert("Device is online");
app.getNetworkState();
}, onOffline: function(){
alert("Device is offline");
}
};
app.initialize();
如果这不起作用,那么您的cordova /插件安装有问题。祝好运。 ; - ))