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

问题描述 投票:1回答:2

在phonegap中创建移动应用程序时,我想在线/离线显示有关互联网连接的警报消息。但我没有得到警报信息。我用了

科尔多瓦 - 插件网络信息

在我的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>

任何人都可以帮我解决这个问题吗?

javascript android cordova phonegap-plugins phonegap
2个回答
1
投票

首先添加以下Cordova插件:

cordova plugin add cordova-plugin-network-information

cordova plugin add cordova-plugin-dialogs

添加以下cript文件:

jquery.mobile-1.4.0.css jQuery的1.10.2.min.js cordova.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更多详情点击Here..!


0
投票

在添加上述代码之前,您必须确保已加载文档。在HTML中执行以下操作:

<body onload="onLoad">

并在您的脚本/ JS文件中执行以下操作:

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

function onDeviceReady() {
   //call your internet connectivity code here...
}
© www.soinside.com 2019 - 2024. All rights reserved.