|
说明:
该属性是确定设备网络连接状态和连接类型的快速方法。
支持的平台:
iOS
Android
BlackBerry WebWorks (OS 5.0或更高版本)
Windows Phone 7 ( Mango )
简单的范例:- 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';
-
- alert('Connection type: ' + states[networkState]);
- }
-
- checkConnection();
复制代码 完整的范例:
- <!DOCTYPE HTML>
- <html>
- <head>
- <meta charset="utf-8">
- <title>phonegap-NetWork</title>
- <link rel="stylesheet" href="../jquery.mobile-1.1.1/jquery.mobile-1.1.1.css" />
- <script src="../jquery.mobile-1.1.1/jquery.js"></script>
- <script src="../jquery.mobile-1.1.1/jquery.mobile-1.1.1.js"></script>
- <script type="text/javascript" charset="utf-8" src="../cordova-1.9.0.js"></script>
- <script type="text/javascript">
- $(document).ready(function(){
- document.addEventListener("deviceready", myDeviceReadyListener, false);
- });
-
- function myDeviceReadyListener(){
- var networkState = navigator.network.connection.type;
- var states = {};
- states[Connection.UNKNOWN]= "未知网络";
- states[Connection.ETHERNET]= "蓝牙网络";
- states[Connection.WIFI]= "WIFI无线网络";
- states[Connection.CELL_2G]= "2G网络";
- states[Connection.CELL_3G]= "3G网络";
- states[Connection.CELL_4G]= "4G网络";
- states[Connection.NONE]= "无网络";
-
- $("#networkState .ui-btn-text").text(states[networkState]);
- }
-
- </script>
- </head>
- <body>
- <div data-role="page">
- <div data-role="header">
- <h1>PhoneGap实战</h1>
- </div>
- <div data-role="content">
- <a href="#" data-role="button" id="networkState">网络获取中....</a>
- </div>
- <div data-role="footer">
- <h4> </h4>
- </div>
- </div>
- </body>
- </html>
复制代码 |
|