2013-07-19 60 views
2

我正在尝试使用Phonegap制作一个简单的应用程序,并使用Adobe Phonegap生成器进行编译。我已经找到并使用了充分记录的示例来使用下面的navigator.connection.type,并在其中添加了另一行,以在设备准备就绪时生成警告框。它甚至没有那么远。我曾在某些时候通过将代码从头部移动到页面主体来显示无尽的旋转圈,但最终没有帮助。在iOs和Android设备上进行测试的结果相同,并且config.xml包含: -即使设备准备就绪,navigator.connection.type也不能正常工作*或*设备从未准备好

<plugin name="NetworkStatus" value="CDVConnection" /> 
<plugin name="NetworkStatus" value="org.apache.cordova.NetworkManager" /> 

任何帮助极大的赞赏。

// Wait for Cordova to load 
// 
document.addEventListener("deviceready", onDeviceReady, false); 

// Cordova is loaded and it is now safe to make calls Cordova methods 
// 
function onDeviceReady() { 
    alert('Device is ready'); 
    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]); 
} 

</script> 

回答

6

你也应该等到所有的脚本加载。裹一切都在一个onBodyLoad像这样:

function onBodyLoad() { 
    // these are useful later in the app, might as well set early 
    window.isRipple = (window.tinyHippos != null); 
    window.isPhoneGap = /^file:\/{3}[^\/]/i.test(window.location.href); 
    window.isIOS = !window.isRipple && navigator.userAgent.match(/(ios|iphone|ipod|ipad)/gi) != null; 
    window.isAndroid = !window.isRipple && navigator.userAgent.match(/(android)/gi) != null; 

    // stuff I use for debugging in chrome 
    if (window.isPhoneGap) { 
     document.addEventListener("deviceready", onDeviceReady, false); 
    } else { 
     onDeviceReady(); 
    } 
} 

,并加入到你的身体标记:

<body onload="onBodyLoad()"> 

和我的代码额外引用的其余部分:

function checkOffLine(minutes) { 
    if (window.lastCheckTime == null) { 
     window.lastCheckTime = 0; 
    } 

    var currentTime = (new Date()).getTime(); 
    if (currentTime < (window.lastCheckTime + minutes * 60000)) return; 
    window.lastCheckTime = currentTime; 

    // ios does not allow you to exit the application so just warn 
    // ios also require you to warn or your app get rejected 
    if (window.isIOS) { 
     navigator.notification.alert('This application may not function properly without an internet connection.'); 
    } else { 
     navigator.notification.confirm(
      'This application may not function properly without an internet connection. Continue working offline?', // message 
      function(button)  // callback to invoke with index of button pressed 
      { 
       if (button == 1) { 
        navigator.app.exitApp(); 
       } 
      }, 
       'Warning', // title 
       'Exit,Continue'      // buttonLabels 
      ); 
     } 
} 

function checkConnection() { 
    // your check connection type code here or just use navigator.onLine 
    if (!navigator.onLine) { 
     // don't be annoying, only confirm for once every every 5 minutes 
     checkOffLine(5); 
    } 
} 

// initial phonegap deviceready handler 
function onDeviceReady() { 
    console.log('Application started'); 
    angular.bootstrap(document, ['assetsApp']); 
    if (window.isPhoneGap) { 
     document.addEventListener("offline", checkConnection, false); 
     checkConnection(); 
    } 
}; 

function onBodyLoad() { 
    // these are useful later in the app, might as well set early 
    window.isRipple = (window.tinyHippos != null); 
    window.isPhoneGap = /^file:\/{3}[^\/]/i.test(window.location.href); 
    window.isIOS = !window.isRipple && navigator.userAgent.match(/(ios|iphone|ipod|ipad)/gi) != null; 
    window.isAndroid = !window.isRipple && navigator.userAgent.match(/(android)/gi) != null; 

    // stuff I use for debugging in chrome 
    if (window.isPhoneGap) { 
     document.addEventListener("deviceready", onDeviceReady, false); 
    } else { 
     onDeviceReady(); 
    } 
} 
+0

谢谢,这确实让我感动长一些。我现在已经缩小了问题范围。我现在得到的代码只要运行checkConnection函数(我用一个警报来验证),但它永远不会到达该函数的末尾,以提醒我连接是什么。 – galaxyg

+1

进一步的测试,它在navigator.connection.type下降,我可以提醒这个值并且警报从不发生。 – galaxyg

+1

进一步观察你的代码我发现你正在使用navigator.onLine而不是navigator.connection.type,如果我尝试这样,我会得到一个结果:true。我想这会解决问题,因为我不需要知道类型,只有当我在线或没有。但是为什么连接类型代码不起作用,这是一个难题。 – galaxyg

1

我有同样的问题,发现我必须运行“cordova build”,然后状态才能正确返回。

当心 当我运行科尔多瓦构建,它似乎把一切都在我的〜/应用/ www目录和overried一切都在应用/平台/安卓/资产/ WWW/

我的“安装流程”如下:

cordova create app com.app "App" 
cd app 
cordova platform add android 
cordova plugin add org.apache.cordova.network-information 
cordova plugin add org.apache.cordova.camera 
cordova plugin add org.apache.cordova.geolocation 
cordova build 

然后我就可以做应用程序/网络代码修改和开心的时候,‘部署’它使用‘科尔多瓦建设’(这似乎始终将文件复制到应用/平台/安卓/ assets/www /。

如果我添加使用其他插件:

cordova plugin add org.apache.cordova.file 

(例如),那么我需要运行

cordova build 

有它的工作。

我希望这有助于

(我用科尔多瓦3.3.1-0.1.2)