题记:写这篇博客要主是加深自己对网络界面的认识和总结实现算法时的一些验经和训教,如果有错误请指出,万分感谢。

取得网络连接状态

随着3G和Wifi的推广,越来越多的Android应用程序须要调用网络资源,检测网络连接状态也就成为网络应用程序所必备的功能。

Android平台提供了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 calling

Context.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

上面这个简单的例子 checkNetworkInfo() 说明了如何编程取得Android手机的当前网络状态 每日一道理
自己把自己说服了,是一种理智的胜利;自己被自己感动了,是一种心灵的升华;自己把自己征服了,是一种人生的成功。
 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()); //显示3G网络连接状态        //wifi        State wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();        txtWifi.setText(wifi.toString()); //显示wifi连接状态    }

注:

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

运行结果(关闭3G及wifi网络连接的状态下)

调用Android手机的网络配置界面

使用过Android手机上的手机QQ的朋友,应该晓得,当QQ启动时,如果没有有效的网络连接,QQ会提示转入手机的网络配置界面。这是如何实现的呢。

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网络设置界面            }

运行结果(关闭3G及wifi网络连接的状态下),程序转入无线网络配置界面
startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));//进入无线网络配置界面

如果调用

startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS)); //直接进入手机中的wifi网络设置界面

则直接进入手机中的wifi网络设置界面

在wifi网络连接后 运行该程序

我们可以看到 wi-fi 状态为已连接(CONNECTED).


文章结束给大家分享下程序员的一些笑话语录: 那是习惯决定的,一直保持一个习惯是不好的!IE6的用户不习惯多标签,但是最终肯定还是得转到多标签的浏览器。历史(软件UI)的进步(改善)不是以个人意志(习惯)为转移的!

更多相关文章

  1. Android(安卓)如何获取所有的wifi连接历史记录?
  2. 快速开发框架Afinal的使用(数据库操作,HTTP请求,网络图片加载,控件绑
  3. Android之WebView优化之路
  4. Android自定义控件一简介
  5. Android实践之简易天气(二)
  6. Android网络相关---上网流程
  7. Android网络编程之Http通信
  8. Android中的网络时间同步 !!!!!!!!
  9. Android(安卓)Service创建USB HOST通信

随机推荐

  1. android面试题总结
  2. Android开发-直播视讯(3)-创建一个Ubuntu
  3. findViewById()返回布局XML中自定义组件
  4. 基于Android6.0的RIL底层模块分析
  5. 在Webview上加载脱机更新页面
  6. 如何使用java RESTful Web服务将通知推送
  7. Java项目无法通过BufferedWriter向服务器
  8. Android无法加载第三方so的解决方法
  9. Android 打包VersionCode自增、APK文件名
  10. Android6.0的phone应用源码分析(5)——RIL