2

I have one mobile. i need test mobile have internet or not. I tried like this

function onLoad() {
    document.addEventListener("online", onOnline, false);
    document.addEventListener("deviceready", onDeviceReady, false);
}
function onDeviceReady() {
alert("selectitem");
}
function onOnline() {
alert("deivce have enternet");
}

and we tried other way like this

if (navigator.onLine) {
}

both methods are not working for me.

We developing Phone-Gap app. after install app will be need to check mobile have Internet or not.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • @Omar if you anything about my problem please help me.give me any idea – Pavan Alapati Jan 02 '15 at 11:19
  • If you are working on a hybrid mobile application then you need to enable Internet access in Cordova configuration files. – Gajotres Jan 02 '15 at 11:29
  • @Gajotres we developing Phone gap application.We add AndroidManifest.xml like this – Pavan Alapati Jan 02 '15 at 11:31
  • Is it Phonegap or Cordova? There's a big difference. – Gajotres Jan 02 '15 at 11:32
  • 1
    If you are working with Cordova you can use this plugin to detect internet connection and connection type: https://github.com/apache/cordova-plugin-network-information – Gajotres Jan 02 '15 at 11:32
  • @Gajotres PhoneGap application .if you know how to solve my problem Please help me. – Pavan Alapati Jan 02 '15 at 11:33
  • try using window.navigator.onLine instead of navigator.onLine – Gajotres Jan 02 '15 at 11:35
  • As @Gajotres said - you should add the network info plugin. Can you confirm you did that? – Raymond Camden Jan 02 '15 at 18:32
  • Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on [so]. See "[Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts). BTW, it's "Thanks in advance", not "Thanks in advanced". – John Saunders Jan 24 '15 at 00:50

1 Answers1

0

You should be able to check navigator.connection.type as documented here.

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

// device APIs are available
//
function onDeviceReady() {
    var isOffline = navigator.connection.type === Connection.NONE;
}

Checking that property will get you the network state at the time you check it. You can also use the online and offline events to detect when the connection status changes.

Make sure you:

  1. include the network-information plugin during build
  2. include the correct app permissions to access network state
  3. don't try to check navigator.connection.type until after the deviceready event.
CodingWithSpike
  • 42,906
  • 18
  • 101
  • 138