I use navigator.network.connection.typeto get device network status. But he shows an error
TypeError: Result of expression 'navigator.network' [undefined] is not an object.
I tried with Phonegap 1.0.0, Cordova 1.7.0, Cordova 1.7.0rc1, but I still get the same error.
It works fine on iOS, but not on Android. Can someone help me with this?
Here is my code:
<script type="text/javascript" charset="utf-8">
document.addEventListener("deviceready",onDeviceReady,false);
function onDeviceReady() {
checkConnection();
loaddb();
}
function checkConnection() {
alert(navigator.network);
if(navigator.network==undefined) {
window.localStorage.setItem("internetAccessFlag","false");
} else {
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';
if(networkState==Connection.UNKNOWN || networkState==Connection.NONE) {
window.localStorage.setItem("internetAccessFlag","false");
} else {
window.localStorage.setItem("internetAccessFlag","true");
}
}
}
</script>
EDIT: - I am using Cordova 1.7.0, and plugins.xml has
<plugin name="NetworkStatus" value="org.apache.cordova.NetworkManager"/>
Is there anything I have to do with this?
source
share