这个类是用来查询设备的网络连接状态的。当网络连接发生变化时,也会去提醒程序。通过Context.getSystemService(Context.CONNECTIVITY_SERVICE)获取实例
这个类有以下几个职责:
1 管理网络连接(wifi,GPRS,UMTS,etc
2 网络发生变化时发送广播
3当网络失去连接时,尝试”fail over“到另一个网络连接
4Provide an API that allows applications to query the coarse-grained or fine-grained state of the available networks
5提供用户查询和选择数据交换网络方式的API
public static final String ACTION_BACKGROUND_DATA_SETTING_CHANGED
This constant was deprecated in API level 16.
As of ICE_CREAM_SANDWICH
, availability of background data depends on several combined factors, and this broadcast is no longer sent. Instead, when background data is unavailable, getActiveNetworkInfo()
will now appear disconnected. During first boot after a platform upgrade, this broadcast will be sent once if getBackgroundDataSetting()
was false
before the upgrade.
Broadcast Action: The setting for background data usage has changed values. Use getBackgroundDataSetting()
to get the current value.
If an application uses the network in the background, it should listen for this broadcast and stop using the background data if the value isfalse
.
public static final String CONNECTIVITY_ACTION
A change in network connectivity has occurred. A default connection has either been established or lost. The NetworkInfo for the affected network is sent as an extra; it should be consulted to see what kind of connectivity event occurred.
If this is a connection that was the result of failing over from a disconnected network, then the FAILOVER_CONNECTION boolean extra is set to true.
For a loss of connectivity, if the connectivity manager is attempting to connect (or has already connected) to another network, the NetworkInfo for the new network is also passed as an extra. This lets any receivers of the broadcast know that they should not necessarily tell the user that no data traffic will be possible. Instead, the receiver should expect another broadcast soon, indicating either that the failover attempt succeeded (and so there is still overall data connectivity), or that the failover attempt failed, meaning that all connectivity has been lost.
For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY is set to true
if there are no connected networks at all.