簡體   English   中英

設備未檢測到在線/離線狀態

[英]Device not detect the online/ offline status

在我的phonegap應用程序中,我想檢查設備的在線/離線狀態。

Cordova version is 3.3
Android version 4.2.2
Jquery Mobile 1.3

所以我使用以下方法來檢測在線/離線。 方法1:

document.addEventListener("deviceready",onDeviceReady,false);

function onDeviceReady(){   
    connectionStatusOnline = navigator.onLine ? 'online' : 'offline';
    alert(connectionStatusOnline);
}

它總是返回離線狀態

方法2:

document.addEventListener("online", toggleon, false);
    document.addEventListener("offline", toggleoff, false);
function toggleon() {
    alert("online");

}
function toggleoff() {
    alert("offline");

}

方法2中,它從不調用toogleon或toogleoff

方法3:

function onDeviceReady() {
//document.addEventListener("offline", toggleCon, false);
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]);
}

在方法3中,它顯示了Connection undefined

我在config.xml and set all the permission in manifest有所有插件config.xml and set all the permission in manifest

看來你的cordova.js和cordova_plugin.js文件在www / js文件夾里面。 So the file API's are called in this staratergy 因此設備未檢測到設備處於離線/在線狀態。 我之前遇到過類似的問題。 我做了什么把cordova.js和cordova_pluins.js文件復制到www 文件夾中

您的project directory should be like www/cordova.js and www/cordova_plugins.js

不是www/js/cordova.js and www/js/cordova_plugin.js

通過CLI添加網絡插件... 鏈接

cordova plugin add org.apache.cordova.network-information

然后

<script>

   document.addEventListener("deviceready", onDeviceReady, false);

    function onDeviceReady()
    {

        if(navigator.connection.type==0)
        {
              toggleoff();  
        }
        else if(navigator.connection.type=='none')
        {
              toggleoff();  
        }
        else
        {
              toggleon();
        }

     }
</script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM