http://www.cnblogs.com/mainroadlee/archive/2011/01/11/Android_Network_State_Checking_And_Setting.html


ndroid平台提供了ConnectivityManager类,用于网络连接状态的检测。

Android开发文档这样描述ConnectivityManager的作用:

Class that answers queries about the state of network connectivity. It also notifies applications when network connectivity changes. Get an instance of this class by callingContext.getSystemService(Context.CONNECTIVITY_SERVICE).

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

注:

根据Android的安全机制,在使用ConnectivityManager时,必须在AndroidManifest.xml中添加<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 否则无法获得系统的许可。





private void checkNetworkInfo()    {        ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);        //mobile 3G Data Network        State mobile = conMan.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();        txt3G.setText(mobile.toString());        //wifi        State wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();        txtWifi.setText(wifi.toString());                //如果3G网络和wifi网络都未连接,且不是处于正在连接状态 则进入Network Setting界面 由用户配置网络连接        if(mobile==State.CONNECTED||mobile==State.CONNECTING)            return;        if(wifi==State.CONNECTED||wifi==State.CONNECTING)            return;                        startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));//进入无线网络配置界面        //startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS)); //进入手机中的wifi网络设置界面            }


更多相关文章

  1. Android(安卓)获取网络时间
  2. android adb 通过adb连接制定设备
  3. 浅析android下如何通过jni监控wifi网络连接、dhcpcd执行和power
  4. Android(安卓)studio无法更新 提示网络连接失败
  5. Android瀑布流 & 仿Win8的metro的UI界面
  6. android中判断网络和WIFI是否连接
  7. android中判断网络和WIFI是否连接
  8. android之网络请求 -- 获取RecyclerView的列表项(图片 + 文字)
  9. android中判断网络和WIFI是否连接

随机推荐

  1. 服务器Tomcat,你会安装了吗?
  2. 聊一聊我们应该如何有效学习
  3. 熔断器 Hystrix 源码解析 —— 命令执行(
  4. 数据库分库分表中间件 Sharding-JDBC 源
  5. 【招聘】杭州阿里招高级前端
  6. 【招聘】上海携程门票业务招中高级前端
  7. 分布式消息队列 RocketMQ 源码分析 ——
  8. 熔断器 Hystrix 源码解析 —— 执行结果
  9. 【第557期】搭建前端私有npm杂记
  10. 【第764期】你不懂JS:this是什么?