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)。
网络改变时发送广播,使用CONNECTIVITY_ACTION注册Receiver接收广播
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里的几种状态
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();
相关文章推荐
- Linux的时间设置与同步(NTP)
- Could not create the view: An unexpected exception was thrown.问题解决
- mysql 远程root赋予创建用户权限
- 更新 Ubuntu 系统,避免报错「hash校验和不符」
- 黑马程序员_JavaSE基础知识总结二十:课堂上关于集合的几个超经典案例(结课前集合知识温故)
- Git教程
- 类之间的关系:泛化、依赖、关联、聚合、合成
- spark介绍
- 铁轨(栈)
- 手机维修技术
- 矩阵乘法的四种理解方式
- 解决VMware虚拟机无法识别U盘问题
- iOS 网络请求图片下载到本地相册
- hibernate缓存详解
- 初学者Oracle 11g安装常见错误
- Java环境变量设置
- Java中数组、对象及其内存管理、回收
- Spring MVC静态资源处理
- 【队列】循环队列
- Android添加新版本支持包V7后自定义ActionBar左侧有空白,不能玩全填充问题解决