ConnectivityManager(三)

来源:互联网 发布:淘宝优惠券链接在哪 编辑:程序博客网 时间:2024/06/07 23:23

转载请标明出处:http://blog.csdn.net/xx326664162/article/details/49385323 文章出自:薛瑄的博客

你也可以查看我的其他同类文章,也会让你有一定的收货!

关于Android WiFi,我写了5篇文章,讲解了在配置WiFi中用到的知识

WifiManager、ScanResult(一)
WifiInfo(二)
ConnectivityManager(三)
NetworkInfo和NetworkInfo.State(四)
WifiManager、WifiInfo、WifiConfiguration集成类(五)

代码示例来自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)。
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
  • 网络改变时发送广播,使用CONNECTIVITY_ACTION注册Receiver接收广播
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里的几种状态
State wifiState = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
1 0
原创粉丝点击