Android进行网络联网的一些操作时,经常会对网络是否已经连接成功进行判断。我们通常会对wifi和移动网络进行判断,我们需要判断网络设备是否开启,是否连接成功,这里做个笔记哈。

        
  1. packagecom.example.util;
  2. importandroid.content.Context;
  3. importandroid.net.ConnectivityManager;
  4. importandroid.net.NetworkInfo;
  5. importandroid.net.wifi.WifiManager;
  6. importandroid.telephony.TelephonyManager;
  7. importandroid.util.Log;
  8. /**
  9. *
  10. *@authorXuZhiwei(xuzw13@gmail.com)
  11. *Weibo:http://weibo.com/xzw1989
  12. *Createat2012-9-22上午11:25:04
  13. */
  14. publicclassNetUtil{
  15. /**
  16. *判断Network是否开启(包括移动网络和wifi)
  17. *
  18. *@return
  19. */
  20. publicstaticbooleanisNetworkEnabled(ContextmContext){
  21. return(isNetEnabled(mContext)||isWIFIEnabled(mContext));
  22. }
  23. /**
  24. *判断Network是否连接成功(包括移动网络和wifi)
  25. *@return
  26. */
  27. publicstaticbooleanisNetworkConnected(ContextmContext){
  28. return(isWifiContected(mContext)||isNetContected(mContext));
  29. }
  30. /**
  31. *判断移动网络是否开启
  32. *
  33. *@return
  34. */
  35. publicstaticbooleanisNetEnabled(Contextcontext){
  36. booleanenable=false;
  37. TelephonyManagertelephonyManager=(TelephonyManager)context
  38. .getSystemService(Context.TELEPHONY_SERVICE);
  39. if(telephonyManager!=null){
  40. if(telephonyManager.getNetworkType()!=TelephonyManager.NETWORK_TYPE_UNKNOWN){
  41. enable=true;
  42. Log.i(Thread.currentThread().getName(),"isNetEnabled");
  43. }
  44. }
  45. returnenable;
  46. }
  47. /**
  48. *判断wifi是否开启
  49. */
  50. publicstaticbooleanisWIFIEnabled(Contextcontext){
  51. booleanenable=false;
  52. WifiManagerwifiManager=(WifiManager)context
  53. .getSystemService(Context.WIFI_SERVICE);
  54. if(wifiManager.isWifiEnabled()){
  55. enable=true;
  56. Log.i(Thread.currentThread().getName(),"isWifiEnabled");
  57. }
  58. Log.i(Thread.currentThread().getName(),"isWifiDisabled");
  59. returnenable;
  60. }
  61. /**
  62. *判断移动网络是否连接成功!
  63. *@paramcontext
  64. *@return
  65. */
  66. publicstaticbooleanisNetContected(Contextcontext){
  67. ConnectivityManagerconnectivityManager
  68. =(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
  69. NetworkInfomobileNetworkInfo=connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
  70. if(mobileNetworkInfo.isConnected())
  71. {
  72. Log.i(Thread.currentThread().getName(),"isNetContected");
  73. returntrue;
  74. }
  75. Log.i(Thread.currentThread().getName(),"isNetDisconnected");
  76. returnfalse;
  77. }
  78. /**
  79. *判断wifi是否连接成功
  80. *@paramcontext
  81. *@return
  82. */
  83. publicstaticbooleanisWifiContected(Contextcontext){
  84. ConnectivityManagerconnectivityManager
  85. =(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
  86. NetworkInfowifiNetworkInfo=connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
  87. if(wifiNetworkInfo.isConnected())
  88. {
  89. Log.i(Thread.currentThread().getName(),"isWifiContected");
  90. returntrue;
  91. }
  92. Log.i(Thread.currentThread().getName(),"isWifiDisconnected");
  93. returnfalse;
  94. }
  95. }

更多相关文章

  1. Android使用Retrofit进行网络请求
  2. Android(安卓)热点开关状态的判断和获取热点ssid
  3. Android中判断网络功能是否可用
  4. Android的网络状态判断
  5. Android打开/关闭数据流量
  6. 重定向android log
  7. Android(安卓)7.0 https/tls证书配置问题
  8. 【android测试】值得学习的android测试知识连接
  9. Android(安卓)startActivityForResult的使用

随机推荐

  1. android spinner 省市级联
  2. android listview拖拽,拖动item 改变位置
  3. android 除法运算保留小数点
  4. 选项卡TabHost
  5. Android(安卓)Studio实现标题栏和状态栏
  6. 【Android 应用开发】 Application 使用
  7. Android(安卓)Phone类分析
  8. Android调用手机浏览器
  9. Android(安卓)系统信息(内存、cpu、sd卡、
  10. Android开发遇到的问题和小知识总结(不断