Detect if phone is online/offline with PHONEGAP reachability API
Hi and thanks in advance,
I'm aware Phonegap has a reachability API and I'd like to know how I can use it to detect if the phone is connected to the network or not.
What I found is here: http://github.com/phonegap/mobi开发者_如何学Pythonle-spec/blob/master/tests/network.tests.js
I just don't know how to use it or if it even suits my needs.
Thanks again.
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>
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
The isReachable example was incorrect and confusing, so I pushed a quick update that demonstrates how to use isReachable on iOS / Android / BlackBerry.
The PhoneGap Documentation is still being actively written and isReachable has open tickets for all of the PhoneGap platforms. If you happen to find an errors (or find that the example is confusing), I'd appreciate a comment on one of the existing isReachable tickets.
Thanks! Michael
精彩评论