下方是一个很好的监测网络、状态的工具类

  1. public class NetworkUtils {
  2. /**
  3. * 网络是否可用
  4. *
  5. * @param activity
  6. * @return
  7. */
  8. public static boolean isNetworkAvailable(Context context) {
  9. ConnectivityManager connectivity = (ConnectivityManager) context
  10. .getSystemService(Context.CONNECTIVITY_SERVICE);
  11. if (connectivity == null) {
  12. } else {
  13. NetworkInfo[] info = connectivity.getAllNetworkInfo();
  14. if (info != null) {
  15. for (int i = 0; i < info.length; i++) {
  16. if (info[i].getState() == NetworkInfo.State.CONNECTED) {
  17. return true;
  18. }
  19. }
  20. }
  21. }
  22. return false;
  23. }
  24. /**
  25. * Gps是否打开
  26. *
  27. * @param context
  28. * @return
  29. */
  30. public static boolean isGpsEnabled(Context context) {
  31. LocationManager locationManager = ((LocationManager) context
  32. .getSystemService(Context.LOCATION_SERVICE));
  33. List<String> accessibleProviders = locationManager.getProviders(true);
  34. return accessibleProviders != null && accessibleProviders.size() > 0;
  35. }
  36. /**
  37. * wifi是否打开
  38. */
  39. public static boolean isWifiEnabled(Context context) {
  40. ConnectivityManager mgrConn = (ConnectivityManager) context
  41. .getSystemService(Context.CONNECTIVITY_SERVICE);
  42. TelephonyManager mgrTel = (TelephonyManager) context
  43. .getSystemService(Context.TELEPHONY_SERVICE);
  44. return ((mgrConn.getActiveNetworkInfo() != null && mgrConn
  45. .getActiveNetworkInfo().getState() == NetworkInfo.State.CONNECTED) || mgrTel
  46. .getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS);
  47. }
  48. /**
  49. * 判断当前网络是否是wifi网络
  50. * if(activeNetInfo.getType()==ConnectivityManager.TYPE_MOBILE) {
  51. *
  52. * @param context
  53. * @return boolean
  54. */
  55. public static boolean isWifi(Context context) {
  56. ConnectivityManager connectivityManager = (ConnectivityManager) context
  57. .getSystemService(Context.CONNECTIVITY_SERVICE);
  58. NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
  59. if (activeNetInfo != null
  60. && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {
  61. return true;
  62. }
  63. return false;
  64. }
  65. /**
  66. * 判断当前网络是否3G网络
  67. *
  68. * @param context
  69. * @return boolean
  70. */
  71. public static boolean is3G(Context context) {
  72. ConnectivityManager connectivityManager = (ConnectivityManager) context
  73. .getSystemService(Context.CONNECTIVITY_SERVICE);
  74. NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
  75. if (activeNetInfo != null
  76. && activeNetInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
  77. return true;
  78. }
  79. return false;
  80. }
  81. }

以上方法均通过测试,tv_result为自设定的TextView。

    1. tv_result.append("网络是否可用:"+NetworkUtils.isNetworkAvailable(MainActivity.this)+"\n");
    2. tv_result.append("GPS开关是否打开:"+NetworkUtils.isGpsEnabled(MainActivity.this)+"\n");
    3. tv_result.append("是否为3G网络:"+NetworkUtils.is3G(MainActivity.this)+"\n");
    4. tv_result.append("WIFI是否打开:"+NetworkUtils.isWifiEnabled(MainActivity.this)+"\n");
    5. tv_result.append("是否为WIFI网络:"+NetworkUtils.isWifi(MainActivity.this)+"\n");

Android工具类-关于网络、状态的工具类的更多相关文章

  1. Android实时监听网络状态

    Android实时监听网络状态(1)   其实手机在网络方面的的监听也比较重要,有时候我们必须实时监控这个程序的实时网络状态,android在网络断开与连接的时候都会发出广播,我们通过接收系统的广播就 ...

  2. Android学习总结——判断网络状态

    package com.example.xch.broadcasttest; import android.content.BroadcastReceiver; import android.cont ...

  3. Android之监测手机网络状态的广播

    Android之监测手机网络状态的广播 Android 监控网络状态 Android利用广播监听设备网络连接(断网)的变化情况

  4. 网络状态诊断工具——netstat命令

    netstat命令可以用来查询整个系统的网络状态.百度百科的定义如下: Netstat的定义是: Netstat是在内核中访问网络连接状态及其相关信息的程序,它能提供TCP连接,TCP和UDP监听,进 ...

  5. Android实时监听网络状态(1)

    其实手机在网络方面的的监听也比较重要,有时候我们必须实时监控这个程序的实时网络状态,android在网络断开与连接的时候都会发出广播,我们通过接收系统的广播就可以实现网络的监听. 1.添加访问网络和获 ...

  6. Android 通过广播获取网络状态

    Android系统网络连接状态的改变会发一个广播,注册一个广播接收者,实时动态的检测网络状态,及时提醒用户,优化用户体验.          本文仅提供WIFI 状态的检测作为参考,其他网络连接方式请 ...

  7. Android实时监听网络状态(2)

    在开发android应用时,涉及到要进行网络访问,时常需要进行网络状态的检查,以提供给用户必要的提醒.一般可以通过ConnectivityManager来完成该工作. ConnectivityMana ...

  8. Android实践--监測网络状态

    Android 监測网络状态      我们在使用Android手机时候,一些APP须要网络环境才干执行,所以手机须要可用的网络,无论是2G.3G或者WIFI.甚至有一些比較耗流量的APP仅仅能在WI ...

  9. iOS完美的网络状态判断工具

    大多数App都严重依赖于网络,一款用户体验良好的的app是必须要考虑网络状态变化的.iOSSinger下一般使用Reachability这个类来检测网络的变化. Reachability 这个是苹果开 ...

  10. Android之判断当前网络状态

    /** * 检测网络是否可用 * @return */ public boolean isNetworkConnected() { ConnectivityManager cm = (Connecti ...

随机推荐

  1. 保存html代码

    function svcode(F) { if (document.all) { var F = $id(F); var E = window.open("", "_bl ...

  2. 017-通过govendor管理依赖包

    1:安装 go get -u github.com/kardianos/govendor 2:配置环境变量 需要把 $GOPATH/bin/ 加到 PATH 中 D:\my_workspace\go_ ...

  3. hdu4403A very hard Aoshu problem 线段树

    //给一个长度为大于2小于15的字符串 //在当中间加'+'或'='使得其成为一个等式的方法的个数 //枚举等号位置.暴力搜索加号加的位置 #include<cstdio> #includ ...

  4. 腾讯云ubuntu搭建tomcat

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6377945.html 一:工具准备 Putty+Xftp5,见上一篇博文:http://www.cnblogs ...

  5. VREP中的二维激光雷达

    目前,轮式机器人的研究中已经大量使用激光雷达辅助机器人的避障导航,考虑到使用成本,一般二维激光雷达使用较多,如下图.由于只能扫描一个平面,如果想用二维激光雷达获取环境三维点云,则需要通过移动机器人或加 ...

  6. Axure 验证码、进度条、分页条(翻页)、搜索框、选项卡

    百度网盘:http://pan.baidu.com/s/1jHP4qsm

  7. loadrunner 脚本开发-参数化之将内容保存为参数、参数数组及参数值获取

    转自:http://blog.sina.com.cn/s/blog_13cc013b50102v49c.html(查看原文) 在VuGen中默认使用{}的字符串称为参数 注意:参数必须在双引号中才能用 ...

  8. 查看Win系统激活状态

    Win键+R调出运行框,在运行框中输入cmd slmgr.vbs -dlv winver            回车后就能看到当前系统的版本 slmgr.vbs -dli 显示:操作系统版本.部分产品 ...

  9. ZOJ 1457 Prime Ring Problem(dfs+剪枝)

     Prime Ring Problem Time Limit: 10 Seconds      Memory Limit: 32768 KB A ring is compose of n circ ...

  10. 推荐一款VS2008代码增强插件——MetalScroll

    时光如水,岁月如歌.虽然现在已经是2013年底马上就要步入2014了,但还是有很多人在使用VS2008开发项目,今天要推荐一款VS2008(同时支持VS2005,但不支持VS2010)代码增强插件给仍 ...