0

我正在使用 phonegap 开发应用程序。在我的应用程序中,我需要检查网络状态。我使用了以下代码,

   function checkConnection() 
{
    var networkState            = navigator.network.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';

    console.log("network------"+networkState);               

    return networkState;             
}

它工作正常,但离线模式也返回 3g(在模拟器中测试)。y'day 也工作正常。此方法有任何问题请指导我...

4

1 回答 1

0
document.addEventListener("deviceready",onDeviceReady,false);
                                       function onDeviceReady() {
                                          pictureSource=navigator.camera.PictureSourceType;
                                          destinationType=navigator.camera.DestinationType;
                                          checkConnection();
                                       }
                                       function checkConnection() {
                                          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';

                                        }

现在要检查连接性,您可以提出以下条件:-

if(networkState == Connection.NONE){
                                                                      navigator.notification.confirm('It looks like your photo could not be shared. Please try again.',onConfirmOne,'Photo Not Shared',['Ok']);
                                         }

还要在 config.xml 中输入以下标记:-

 <feature name="NetworkStatus">
    <param name="ios-package" value="CDVConnection" />
</feature>
于 2014-03-24T06:11:50.153 回答