PhoneGap 在线和离线模式

PhoneGap Online and offline mode

我是 phone gap 的新手,并且已经构建了一个带有命令行界面 (CLI) 的 android 应用程序,但是当函数加载时会提醒我 phone 是在线还是关闭线它失败了。以下是我如何安装模块的代码和详细信息。

我的问题是我收到的最后一个警报是 "OK58" 然后 phone 一直在加载和加载。

Javascript代码

函数 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;

// 设置超时(函数(){ 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"); 

我的htmlheader。上面的代码包含在 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

我认为这就是一切。我怎样才能更好地调试它?有什么想法吗?

您的代码中有一些拼写错误,这应该可以工作(未经测试):

    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 and/or 中的输出。 iOS 使用 Safari,Android 使用 Chrome。

如果你有一个旧的 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 /plugin 安装有问题。祝你好运。 ;-))