Skip to content Skip to sidebar Skip to footer

Phonegap Check Internet Connection On Device Vs Mobile Browser

I am new to Phonegap and JqueryMobile. I need to check the internet connection on device. After seeing some solution on net I got confused about approaches. some solutions is sugge

Solution 1:

PhoneGap have own function to check Internet connection available or not.Here is that official document.

  • Check Internet is Onlinedocument.addEventListener("online", onOnline, false);
  • Check Internet is Offlinedocument.addEventListener("offline", onOffline, false);

You need to set

<plugin name="NetworkStatus" value="org.apache.cordova.NetworkManager" /> permission in config.xml for android.

You have to add some permission in manifest as below,

<uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE" /><uses-permissionandroid:name="android.permission.CHANGE_NETWORK_STATE"></uses-permission><uses-permissionandroid:name="android.permission.CHANGE_WIFI_STATE"></uses-permission><uses-permissionandroid:name="android.permission.INTERNET" />

Solution 2:

Try this

Working example: http://jsfiddle.net/Gajotres/d5XYR/

In this case timer will check internet connection every 100 ms and set final result into a javascript global variable.

Everything depends on this line:

window.navigator.onLine -- it will be falseif the user is offline.

Final solution:

var connectionStatus = false;

$(document).on('pagebeforeshow', '#index', function () {
    setInterval(function () {
        connectionStatus = navigator.onLine ? 'online' : 'offline';
    }, 100);
    $(document).on('click', '#check-connection', function () {
        alert(connectionStatus);
    });
});

Post a Comment for "Phonegap Check Internet Connection On Device Vs Mobile Browser"