您的位置:首页 > 其它

ConnectivityManager(三)

2015-11-14 10:40 330 查看
代码示例来自http://www.cnblogs.com/ywtk/p/3876076.html

ConnectivityManager

android.net.ConnectivityManager

1、用于管理与网络连接相关的操作用于查询网络连接类型和状态

2、网络状态发生改变时通知应用。

主要功能:

The primary responsibilities of this class are to:

1、Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)

2、Send broadcast intents when network connectivity changes

3、Attempt to “fail over” to another network when connectivity to a network is lost

4、Provide an API that allows applications to query the coarse-grained or fine-grained state of the available networks

5、Provide an API that allows applications to request and select networks for their data traffic

通过调用下面函数获得ConnectivityManager的实例:Context.getSystemService(Context.CONNECTIVITY_SERVICE)。

[code]ConnectivityManager cwjManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);


网络改变时发送广播,使用CONNECTIVITY_ACTION注册Receiver接收广播

[code]networkBroadcast = new NetworkBroadcast();
networkfilter = new IntentFilter (ConnectivityManager . CONNECTIVITY_ACTION);
registerReceiver(networkBroadcast, networkfilter);


NetworkInfo getNetworkInfo(int networkType)

Returns connection status information about a particular network type. 查询连接状态的信息关于指定的网络模式

NetworkInfo.State getState()

Reports the current coarse-grained state of the network.

这个是NetworkInfo 的方法,粗粒度报告当前的网络连接状态,这里的粗粒度,就是枚举类型State里的几种状态

[code]State wifiState = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: