最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - navigator.network.connection.type returns null instead of Connection.NONE - Stack Overflow

programmeradmin1浏览0评论

For some reason the Phonegap connection API returns null

function checkConnection() {
    var networkState = navigatorwork.connection.type;
    alert(networkState);
    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';

    document.getElementById('connections').innerHTML = states[networkState];
}

I run this function from the onDeviceReady function which is called when the device is running.

When everything is turned off I get null. Shouldn't I get the Connection.NONE back instead of a null? It's really annoying since I can't check whether the phone has connection or not. It works fine when Wifi is turned on though.

For some reason the Phonegap connection API returns null

function checkConnection() {
    var networkState = navigatorwork.connection.type;
    alert(networkState);
    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';

    document.getElementById('connections').innerHTML = states[networkState];
}

I run this function from the onDeviceReady function which is called when the device is running.

When everything is turned off I get null. Shouldn't I get the Connection.NONE back instead of a null? It's really annoying since I can't check whether the phone has connection or not. It works fine when Wifi is turned on though.

Share Improve this question edited May 19, 2013 at 21:47 Andrew Marshall 97k20 gold badges227 silver badges217 bronze badges asked Jan 23, 2012 at 10:27 JelmerJelmer 2,6932 gold badges29 silver badges45 bronze badges 1
  • 1 Hey, I've read the code it shouldn't ever return null. What is your reproduction scenario for this bug? I've put my phone into airplane mode and it returns "none" correctly. Are you making sure to wait for the "deviceready" event before you make a call to checkConnection()? – Simon MacDonald Commented Jan 23, 2012 at 21:36
Add a ment  | 

6 Answers 6

Reset to default 2

Make sure you have added

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

in AndroidManifest.xml

Have a look at the PhoneGap discussion group: https://groups.google./forum/?fromgroups#!searchin/phonegap/navigatorwork.connection.type/phonegap/oAggxryQzrw/hE8uhwN3ONgJ

If you're using 1.6.0, you need to update res/xml/plugins.xml from

<plugin name="Network Status" value="org.apache.cordova.NetworkManager"/>

to be:

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

(You need to remove the space in "Network Status")

I'm currently using PhoneGap 1.4.1 and Sencha Touch 1.1.1. on Android 2.3.5 (tested with HTC Desire S). It seems that the "navigatorwork.connection.type" statement does effectively return 'null' with Phonegap 1.4.1, even if called after 'deviceready' event has been fired (confirmed after checking the source code). However it perfectly works with Cordova (ex-PhoneGap) 1.5.0. So I suggest to upgrade asap :-)

Eric.

For the cordova version prior to 1.6 you need to use:

<plugin name="Network Status" value="org.apache.cordova.NetworkManager"/>

Instead of:

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

Please change the plugin name to "Network Status" with space!

There is another change when going from pre 2.3.0 to post 2.3.0.

Before Cordova 2.3.0, the Connection object existed at: navigatorwork.connection.

To match the spec, this was changed to navigator.connection in 2.3.0.

navigatorwork.connection still exists, but is now deprecated and will be removed in a future release.

2.7.0 docs

So change this

 var networkState = navigatorwork.connection.type;

To This

 var networkState = navigator.connection.type;

Basically remove the network from your Javascript. Hope this helps.

Use this code:

var networkState = navigator.connection.type;

Instead of:

var networkState = navigatorwork.connection.type;
发布评论

评论列表(0)

  1. 暂无评论