cordova - checking of internet connection of device using phonegap -
trying use cordova 2.0.0 , using code checking of internet connection
document.addeventlistener("deviceready", ondeviceready(), false); function ondeviceready() { alert("ready"); db = window.opendatabase("loginintro", "1.0", "loginintro", 1000000); db.transaction(populatedb, errorcb, successcb); checkconnection(); } function checkconnection() { alert("connection"); network = navigator.network.connection.type; alert("fdfd"); 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'; alert('connection type: ' + states[networkstate]); }
but getting error on line
network = navigator.network.connection.type;
and error is:
04-09 15:20:23.989: e/web console(13329): uncaught typeerror: cannot read property 'connection' of undefined @ file:///android_asset/www/js/lib/helloworldjs/alldatabse.js:14 04-09 15:20:39.419: e/cordovawebview(13329): cordovawebview: timeout error! 04-09 15:20:39.419: d/cordova(13329): cordovawebviewclient.onreceivederror: error code=-6 description=the connection server unsuccessful. url=file:///android_asset/www/index.html
you have small mistake in code. should alert('connection type: ' + states[network]);
in addition, make sure have these permissions:
android
app/res/xml/plugins.xml
<plugin name="networkstatus" value="org.apache.cordova.networkmanager" />
app/androidmanifest.xml
<uses-permission android:name="android.permission.internet" /> <uses-permission android:name="android.permission.access_network_state" /> <uses-permission android:name="android.permission.read_phone_state" />
Comments
Post a Comment