For help with installation, bugs reports or feature requests, please head over to our new forums.
Genuitec Community on GitHub
- This topic has 5 replies, 2 voices, and was last updated 14 years, 1 month ago by
Yann.
-
AuthorPosts
-
YannMemberHello
i try to detect if phone is connected to internet
i try this script from phonegap but he don’t workfunction checkConnection() { var networkState = navigator.network.connection.type; var states = {}; states[Connection.UNKNOWN] = 'Unknown connection'; states[Connection.ETHERNET] = '1'; states[Connection.WIFI] = '1'; states[Connection.CELL_2G] = '1'; states[Connection.CELL_3G] = '1'; states[Connection.CELL_4G] = '1'; states[Connection.NONE] = '0'; if (states[networkState]=='0') { alert(No connection.'); } phoneui.documentReadyHandler = function() { checkConnection(); } }can you help me
of course i generated an ipa file to test on my phone.
thanks
YannJuly 12, 2012 at 11:44 am #328139
KaleMemberHi,
the phoneui.documentReadyHandler = function() is inside of your checkConnection() function. So it is never called.
Try:
phoneui.documentReadyHandler = function() {
checkConnection();
}function checkConnection()
{
var networkState = navigator.network.connection.type;
var states = {}; states[Connection.UNKNOWN] = ‘Unknown connection’;
states[Connection.ETHERNET] = ‘1’;
states[Connection.WIFI] = ‘1’;
states[Connection.CELL_2G] = ‘1’;
states[Connection.CELL_3G] = ‘1’;
states[Connection.CELL_4G] = ‘1’;
states[Connection.NONE] = ‘0’;
if (states[networkState]==’0′)
{
alert(No connection.’);
}
}Didn´t check, hope it works.
Kale
July 12, 2012 at 11:54 am #328140
YannMemberHello Kale
thanks for your answer but it’s an error of copy/paste
in my script my function is outside phoneui.documentReadyHandler = function
Thanks
YannJuly 12, 2012 at 2:23 pm #328146
KaleMemberHi,
the problem could be, that a phonegap function is called, before the phonegap framework is fully loaded.
Try this:
phoneui.documentReadyHandler = function() {
document.addEventListener(“deviceready”, onDeviceReady, false);
}
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’;if (states[Connection.NONE]){
alert(‘No Connection!’);
}alert(states[networkState]);
}Should work now.
Kale
July 13, 2012 at 2:29 am #328166
YannMemberHello Kale
thanks again, now i’ve an alert with “no connection” even though I am connected 🙁
so i test another way by testing a response.statut with xmlHttpRequest with success but i would like useif (req.readyState == 4) { // only if "OK" if (req.status == 200 || req.status == 304) { results = req.responseText; document.getElementById(target).innerHTML = results; } else { noInternet(); } function noInternet() { navigator.notification.vibrate(2500); navigator.notification.confirm( 'Aucune connexion internet trouvée.', // message onConfirm, // callback to invoke with index of button pressed 'Erreur', // title 'Close' // buttonLabels );but nothing happen, an idea?
thanks
YannJuly 13, 2012 at 3:36 am #328170
YannMemberi found my problem
thanks for your help Kale
Yann -
AuthorPosts
