Question

I am trying to check for an established network connection through the phonegap/cordova API, but alas, it is not working.

I added the plugin as explained on the cordova documentation, and phonegap local plugin list gives [phonegap] org.apache.cordova.network-information.

However, simply calling

document.addEventListener("deviceready", function () {
  alert(Object.keys(navigator.connection));
}, false);

results on my phone (Android 4.0.4) always returning 0 and running on an emulator with Android 4.4.2 logs into LogCat that navigator.connection is undefined. In fact, on both machines alert(Object.keys(navigator)); never shows the connection key.

Any solution?

Phonegap with Cordova 3.3.0

Was it helpful?

Solution

When you install the plugin it copies files to www/plugins and updates android.json

then when you build the project, the build copies the java to platforms/android/src/... and the js files to platforms\android\assets\www\plugins (and does any customisation defined in plugin.xml)

At multiple occasions I saw android.json not correctly updated and javascript files not copied. Removing and re-installing plugins solved the problem for me.

If you did not perform manual customisations to the platforms/android folder, I think the easiest way to clean your project would be to empty the /plugins folder and the platforms folder and then re-install all the plugins you need.

OTHER TIPS

I had the same issue and found I had to run "cordova build" and then the status was returned correctly.

BEWARE When I run cordova build, it appears to take everything in my ~/app/www directory and overried everything in app/platforms/android/assets/www/

My "install process" is as follows:

cordova create app com.app "App"
cd app
cordova platform add android
cordova plugin add org.apache.cordova.network-information
cordova plugin add org.apache.cordova.camera
cordova plugin add org.apache.cordova.geolocation
cordova build

I can then do code changes in app/www and when happy, 'deploy' it using 'cordova build' (which seems to always copy the files to app/platforms/android/assets/www/.

If I add another plugin using: (for example)

cordova plugin add org.apache.cordova.file

then I need to run

cordova build

to have it work.

I hope this helps

(I am using cordova 3.3.1-0.1.2 )

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top