PhoneGap API介绍:Connection

移动开发
本文将介绍PhoneGap API——Connection:快速检查WiFi或蜂窝网络的信息。Connection对象提供对设备的蜂窝及WiFi连接信息的访问。通过navigator.network接口访问该对象。

属性

常量

connection.UNKNOWN

connection.ETHERNET

connection.WIFI

connection.CELL_2G

connection.CELL_3G

connection.CELL_4G

connection.NONE

connection.type

检查正在使用的活动网络连接。

说明

该属性是确定设备网络连接状态和连接类型的快速方法。

支持的平台

iOS

Android

BlackBerry WebWorks (OS 5.0或更高版本)

简单的范例

  1. function checkConnection() { 
  2.     var networkState = navigator.network.connection.type; 
  3.      
  4.     var states = {}; 
  5.     states[Connection.UNKNOWN]  = 'Unknown connection'
  6.     states[Connection.ETHERNET] = 'Ethernet connection'
  7.     states[Connection.WIFI]     = 'WiFi connection'
  8.     states[Connection.CELL_2G]  = 'Cell 2G connection'
  9.     states[Connection.CELL_3G]  = 'Cell 3G connection'
  10.     states[Connection.CELL_4G]  = 'Cell 4G connection'
  11.     states[Connection.NONE]     = 'No network connection'
  12.      
  13.     alert('Connection type: ' + states[networkState]); 
  14.      
  15. checkConnection(); 

完整的范例

  1. <!DOCTYPE html> 
  2. <html> 
  3. <head> 
  4. <title>navigator.network.connection.type Example</title>  
  5.  
  6. <script type="text/javascript" charset="utf-8" src="phonegap.js"></script>   
  7. <script type="text/javascript" charset="utf-8">   
  8.  
  9.     // 等待加载PhoneGap 
  10.     document.addEventListener(“deviceready“, onDeviceReady, false);   
  11.  
  12.     // PhoneGap加载完毕,可以安全调用PhoneGap方法 
  13.     function onDeviceReady() {  
  14.         checkConnection();  
  15.     }   
  16.  
  17.     function checkConnection() {  
  18.         var networkState = navigator.network.connection.type;         
  19.  
  20.         var states = {};  
  21.         states[Connection.UNKNOWN]  = 'Unknown connection';  
  22.         states[Connection.ETHERNET] = 'Ethernet connection';  
  23.         states[Connection.WIFI]     = 'WiFi connection';  
  24.         states[Connection.CELL_2G]  = 'Cell 2G connection';  
  25.         states[Connection.CELL_3G]  = 'Cell 3G connection';  
  26.         states[Connection.CELL_4G]  = 'Cell 4G connection';  
  27.         states[Connection.NONE]     = 'No network connection';  
  28.  
  29.         alert('Connection type: ' + states[networkState]);  
  30.  
  31. </script> 
  32. </head> 
  33. <body> 
  34.     <p>A dialog box will report the network state.</p> 
  35. </body> 
  36. </html> 

 

责任编辑:佚名 来源: PhoneGap中国
相关推荐

2011-12-30 14:05:17

PhoneGap APConnection视频

2011-12-19 16:09:32

PhoneGap APCamera

2011-12-20 11:20:46

PhoneGap APCompass

2011-12-21 21:56:45

PhoneGap APFile

2011-12-20 17:15:52

PhoneGap APEvents

2011-12-19 15:30:25

AccelerometPhoneGap AP

2011-12-20 13:32:52

PhoneGap APContacts

2011-12-20 16:09:57

PhoneGap APDevice

2011-12-22 09:54:40

PhoneGap APMedia

2011-12-22 09:27:36

PhoneGap APGeolocation

2011-12-19 16:26:39

PhoneGap APCapture

2011-12-22 10:33:39

PhoneGap APNotificatio

2011-12-22 10:45:32

PhoneGap APStorage

2011-12-30 13:47:57

PhoneGap AP视频Contacts

2011-12-30 13:59:38

PhoneGap APDevice视频

2011-12-30 14:09:32

PhoneGap APCompass视频

2011-12-30 14:13:05

PhoneGap APCamera视频

2011-12-30 14:16:02

AccelerometPhoneGap AP视频

2011-09-13 11:06:08

PhoneGap AP

2011-07-05 17:29:53

PhoneGapevents
点赞
收藏

51CTO技术栈公众号