简体   繁体   English

使用 phonegap 检查移动应用程序中的互联网连接

[英]Check internet connectivity in mobile application using phonegap

On creating the mobile app in phonegap, I would like to show an alert message regarding internet connectivity as online / offline.在 phonegap 中创建移动应用程序时,我想显示有关互联网连接为在线/离线的警报消息。 But I do not get the alert message.但我没有收到警报消息。 I used我用了

cordova-plugin-network-information科尔多瓦插件网络信息

plugin with the code below on my js file在我的 js 文件中使用以下代码的插件

<script type="text/javascript">

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>

Can any one help me with the solution?任何人都可以帮助我解决问题吗?

First Add the below Cordova Plugins:首先添加以下 Cordova 插件:

cordova plugin add cordova-plugin-network-information 科尔多瓦插件添加科尔多瓦插件网络信息

cordova plugin add cordova-plugin-dialogs 科尔多瓦插件添加科尔多瓦插件对话框

Add Below cript files:添加以下脚本文件:

jquery.mobile-1.4.0.css jquery.mobile-1.4.0.css
jquery-1.10.2.min.js jquery-1.10.2.min.js
cordova.js科尔多瓦.js

  <!DOCTYPE html>
    <html>
    <head>
        <meta http-equiv="Content-type" name="viewport"
            content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no, width=device-width">
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <meta name="viewport"
            content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <meta name="format-detection" content="telephone=no">

        <link rel="stylesheet" href="css/jquery.mobile-1.4.0.css" />
        <script src="cordova.js"></script>
        <script type="text/javascript" src="js/jquery-1.10.2.min.js">

</script>
        <script>
            $(document).ready(function () {
                document.addEventListener("deviceready", onDeviceReady, false);
                document.addEventListener("online", onOnline, false);
                document.addEventListener("offline", onOffline, false);
                function onDeviceReady() {
                    console.log(" ready");
                }

                function onOnline() {
                    console.log("connected");
                }

                function onOffline() {
                    debugger;
                    console.log("lost connection");

                    navigator.notification.alert(
                        'Please Check your internet connection.', // message
                        null, // callback
                        'Sample', // title
                        'OK' // buttonName
                    );
                }
            });
        </script>
    </head>
    <body>
        <p style="top:100px;"> Sample</p>
    </body>
    </html>

Output:输出:
输出 For More Details Click Here..!更多详情请点击这里..!

You have to make sure that document has loaded before adding the above code.在添加上述代码之前,您必须确保文档已加载。 Do something like this in your HTML:在你的 HTML 中做这样的事情:

<body onload="onLoad">

and in your script/JS file do something like this:并在您的脚本/JS 文件中执行以下操作:

function onLoad() {
    document.addEventListener("deviceready", onDeviceReady, false);
}

function onDeviceReady() {
   //call your internet connectivity code here...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM