- 文档
- Architectural overview of Cordova platform
- 创建你的第一个App
- Cordova平台支持
- Android平台指南
- Blackberry 10 Guides
- iOS Platform Guide
- OS X 平台指南
- Ubuntu Platform Guide
- Windows Platform Guide
- WP8 Guides
- 平台和插件版本管理
- 自定义app图标(Icon)
- 存储(Storage)
- 隐私指南
- 安全指南
- 白名单指南
- Plugin Development Guide
- Android Plugins
- BlackBerry 10 Plugins
- iOS Plugin Development Guide
- Windows Plugins
- Windows Phone 8 Plugins
- Using Plugman to Manage Plugins
- Embedding WebViews
- Next Steps
- Config.xml
- 事件
- CLI Reference
- Hooks Guide
- Plugin.xml reference documentation
- Battery Status
- Camera
- Console
- Contacts
- Device
- Device Motion
- Device Orientation
- Dialogs
- File
- File Transfer
- Geolocation
- Globalization
- Inappbrowser
- Media
- Media Capture
- Network Information
- Splashscreen
- Vibration
- Statusbar
- Whitelist
- Legacy Whitelist
Network Information
This plugin provides an implementation of an old version of the Network Information API. It provides information about the device's cellular and wifi connection, and whether the device has an internet connection.
Report issues with this plugin on the Apache Cordova issue tracker.
Installation
cordova plugin add cordova-plugin-network-information
Supported Platforms
- Amazon Fire OS
- Android
- BlackBerry 10
- Browser
- iOS
- Windows Phone 7 and 8
- Tizen
- Windows
- Firefox OS
The
connection
object, exposed vianavigator.connection
, provides information about the device's cellular and wifi connection.
Properties
- connection.type
Constants
- Connection.UNKNOWN
- Connection.ETHERNET
- Connection.WIFI
- Connection.CELL_2G
- Connection.CELL_3G
- Connection.CELL_4G
- Connection.CELL
- Connection.NONE
connection.type
This property offers a fast way to determine the device's network connection state, and type of connection.
Quick Example
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]);
}
checkConnection();
API Change
Until Cordova 2.3.0, the Connection
object was accessed via navigator.network.connection
, after which it was changed to navigator.connection
to match the W3C specification. It's still available at its original location, but is deprecated and will eventually be removed.
iOS Quirks
- <iOS7 can't detect the type of cellular network connection.
navigator.connection.type
is set toConnection.CELL
for all cellular data.
Windows Phone Quirks
When running in the emulator, always detects
navigator.connection.type
asConnection.UNKNOWN
.Windows Phone can't detect the type of cellular network connection.
navigator.connection.type
is set toConnection.CELL
for all cellular data.
Windows Quirks
- When running in the Phone 8.1 emulator, always detects
navigator.connection.type
asConnection.ETHERNET
.
Tizen Quirks
- Tizen can only detect a WiFi or cellular connection.
navigator.connection.type
is set toConnection.CELL_2G
for all cellular data.
Firefox OS Quirks
- Firefox OS can't detect the type of cellular network connection.
navigator.connection.type
is set toConnection.CELL
for all cellular data.
Browser Quirks
- Browser can't detect the type of network connection.
navigator.connection.type
is always set toConnection.UNKNOWN
when online.
offline
The event fires when an application goes offline, and the device is not connected to the Internet.
document.addEventListener("offline", yourCallbackFunction, false);
Details
The offline
event fires when a previously connected device loses a network connection so that an application can no longer access the Internet. It relies on the same information as the Connection API, and fires when the value of connection.type
becomes NONE
.
Applications typically should use document.addEventListener
to attach an event listener once the deviceready
event fires.
Quick Example
document.addEventListener("offline", onOffline, false);
function onOffline() {
// Handle the offline event
}
iOS Quirks
During initial startup, the first offline event (if applicable) takes at least a second to fire.
Windows Phone 7 Quirks
When running in the Emulator, the connection.status
is always unknown, so this event does not fire.
Windows Phone 8 Quirks
The Emulator reports the connection type as Cellular
, which does not change, so the event does not fire.
online
This event fires when an application goes online, and the device becomes connected to the Internet.
document.addEventListener("online", yourCallbackFunction, false);
Details
The online
event fires when a previously unconnected device receives a network connection to allow an application access to the Internet. It relies on the same information as the Connection API, and fires when the connection.type
changes from NONE
to any other value.
Applications typically should use document.addEventListener
to attach an event listener once the deviceready
event fires.
Quick Example
document.addEventListener("online", onOnline, false);
function onOnline() {
// Handle the online event
}
iOS Quirks
During initial startup, the first online
event (if applicable) takes at least a second to fire, prior to which connection.type
is UNKNOWN
.
Windows Phone 7 Quirks
When running in the Emulator, the connection.status
is always unknown, so this event does not fire.
Windows Phone 8 Quirks
The Emulator reports the connection type as Cellular
, which does not change, so events does not fire.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论