Detect if phone is online/offline with PHONEGAP reachability API

13,314

Solution 1

http://docs.phonegap.com/en/2.1.0/cordova_connection_connection.md.html#Connection

I noticed you had a few questions regarding phoneGap, there is alot of information in there documentation that I have linked to above... enjoy

Solution 2

With the newest update, the method for checking if the device is online has changed - check out the API documentation at http://docs.phonegap.com/phonegap_connection_connection.md.html#Connection.

Here is the code that they use as an example:

<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
<script type="text/javascript" charset="utf-8">

// Wait for PhoneGap to load
// 
document.addEventListener("deviceready", onDeviceReady, false);

// PhoneGap is loaded and it is now safe to make calls PhoneGap methods
//
function onDeviceReady() {
    checkConnection();
}

function checkConnection() {
    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';

    alert('Connection type: ' + states[networkState]);
}

</script>
Share:
13,314
StJimmy
Author by

StJimmy

Updated on June 05, 2022

Comments