Checking internet connection with phonegap

Asked

Viewed 2,934 times

0

I took this model from the Phonegap website and yet it doesn’t issue an alert showing the status of the connection. I want to open the Inappbrowser that I am creating it show if it is connected on the internet via 3g or wifi and if it is not it emits an alert warning the user.

Example: Example taken from the Phonegap website

It does not issue the alert warning the state of the connection, how to do this?

<!DOCTYPE html>
<html>
  <head>
    <title>navigator.connection.type Example</title>
    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
    <script type="text/javascript" charset="utf-8">

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

    // device APIs are available
    function onDeviceReady() {
        checkConnection();
    }

    function checkConnection() {
        var networkState = navigator.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.CELL]     = 'Cell generic connection';
        states[Connection.NONE]     = 'No network connection';

        alert('Connection type: ' + states[networkState]);
    }
    </script>
  </head>
  <body>
    <p>A dialog box will report the network state.</p>
  </body>
</html>
  • 1

    how’s your code?

  • Math, is the way it is on the site really, Nest elink ai... I just copied and pasted in the application, I circled it in the emulating Eclipse on the connected smartphone on the internet and not emit any alert.

  • It would help if you put it right here in the body of your question, not always people click on the link to see or the link has much more information than necessary.

  • I’m new here Math, I didn’t put the code, because I don’t know how to put it... Do you help me? How do I put an HTML code here in the question?

  • Click edit, copy and paste it into your question, then you select your code and click on the icon {} (code sample), if you are having difficulties we repair, but try.

  • Okay, there’s the code.

  • To check internet connection, one can use a Ping.

  • In the documentation it says that before version 2.2.0, the object connection was in navigator.network.connection... maybe that’s what.

  • Miguel, this example has already been taken from version 3.0, the site is well organized and contains an example for each version... I’m using version 3.0 and I took the example of version 3.0, it would not have why not work, this site of Phonegap is very messed up!

Show 4 more comments

1 answer

1


I checked your code and everything works normally, do the following change the function onDeviceReady to the following

    function onDeviceReady() {
        alert("Device Ready");
        checkConnection();
    }

And check if any message appears on the screen, in case nothing appears the problem should be occurring even before the function of checking the connection. If applicable use adb to check Cordova logs.

To do so open the terminal and enter the following command. adb logcat ":S Cordovalog: Cordova:*"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.