【问题标题】:Device not detect the online/ offline status设备未检测到在线/离线状态
【发布时间】:2014-06-19 09:06:33
【问题描述】:

在我的 phonegap 应用程序中,我想检查设备的在线/离线状态。

Cordova version is 3.3
Android version 4.2.2
Jquery Mobile 1.3

所以我使用以下方法来检测在线/离线。 方法一:

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

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

它总是返回离线

方法二:

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

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

}

方法 2 中它从不调用 toogleon 或 toogleoff

方法三:

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 中的所有插件我都有。

【问题讨论】:

  • 你能告诉我你的项目结构吗(意思是www文件夹结构)。
  • @Arut html 文件位于 www/html 文件夹中,js 文件位于 www/js 和 css 文件 www/css
  • 好的,cordova.js 和 cordova_pluins.js 文件在哪里..
  • @Arut 在我的 www/js 文件中。

标签: javascript android jquery jquery-mobile cordova


【解决方案1】:

您的 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

【讨论】:

    【解决方案2】:

    通过CLI添加网络插件...Link

    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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-15
      • 1970-01-01
      • 2019-06-23
      • 1970-01-01
      • 2017-01-23
      • 2015-05-13
      • 2017-03-13
      相关资源
      最近更新 更多