android中检测网络连接状态简单总结

分类:Android 143人阅读 评论(0) 收藏 举报 android 检测网络连接 wifi 3G GPS

相应几乎没有不跟网络打交道的android应用,那么在实际中就需求检测手机是否有网络连接,甚至需要判断是何种方式连接,这样能给用户带来更好的体验和一些使用指导,下面给出一些常用的判断,如果要知道是否有网络、以及是采用wifi连接的还是3G连接的,调用下面对应方法模型就OK了,代码如下:

TestNetworkActivity:

[java] view plain copy
  1. packagecom.home.testnetwork;
  2. importjava.util.List;
  3. importandroid.app.Activity;
  4. importandroid.content.Context;
  5. importandroid.location.LocationManager;
  6. importandroid.net.ConnectivityManager;
  7. importandroid.net.NetworkInfo;
  8. importandroid.os.Bundle;
  9. importandroid.view.View;
  10. importandroid.view.View.OnClickListener;
  11. importandroid.widget.Button;
  12. importandroid.widget.EditText;
  13. publicclassTestNetworkActivityextendsActivityimplementsOnClickListener{
  14. privateButtoncheckBtn;
  15. privateEditTextnetText;
  16. privateEditTextwifiText;
  17. privateEditTextnet3gText;
  18. privateEditTextgpsText;
  19. @Override
  20. protectedvoidonCreate(BundlesavedInstanceState){
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.main);
  23. checkBtn=(Button)findViewById(R.id.main_btn_check);
  24. checkBtn.setOnClickListener(this);
  25. wifiText=(EditText)findViewById(R.id.main_et_wifi);
  26. net3gText=(EditText)findViewById(R.id.main_et_3g);
  27. gpsText=(EditText)findViewById(R.id.main_et_GPS);
  28. netText=(EditText)findViewById(R.id.main_et_net);
  29. }
  30. /**
  31. *检测网络是否连接
  32. *
  33. *@return
  34. */
  35. privatebooleanisNetConnected(){
  36. ConnectivityManagercm=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
  37. if(cm!=null){
  38. NetworkInfo[]infos=cm.getAllNetworkInfo();
  39. if(infos!=null){
  40. for(NetworkInfoni:infos){
  41. if(ni.isConnected()){
  42. returntrue;
  43. }
  44. }
  45. }
  46. }
  47. returnfalse;
  48. }
  49. /**
  50. *检测wifi是否连接
  51. *
  52. *@return
  53. */
  54. privatebooleanisWifiConnected(){
  55. ConnectivityManagercm=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
  56. if(cm!=null){
  57. NetworkInfonetworkInfo=cm.getActiveNetworkInfo();
  58. if(networkInfo!=null
  59. &&networkInfo.getType()==ConnectivityManager.TYPE_WIFI){
  60. returntrue;
  61. }
  62. }
  63. returnfalse;
  64. }
  65. /**
  66. *检测3G是否连接
  67. *
  68. *@return
  69. */
  70. privatebooleanis3gConnected(){
  71. ConnectivityManagercm=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
  72. if(cm!=null){
  73. NetworkInfonetworkInfo=cm.getActiveNetworkInfo();
  74. if(networkInfo!=null
  75. &&networkInfo.getType()==ConnectivityManager.TYPE_MOBILE){
  76. returntrue;
  77. }
  78. }
  79. returnfalse;
  80. }
  81. /**
  82. *检测GPS是否打开
  83. *
  84. *@return
  85. */
  86. privatebooleanisGpsEnabled(){
  87. LocationManagerlm=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
  88. List<String>accessibleProviders=lm.getProviders(true);
  89. for(Stringname:accessibleProviders){
  90. if("gps".equals(name)){
  91. returntrue;
  92. }
  93. }
  94. returnfalse;
  95. }
  96. @Override
  97. publicvoidonClick(Viewv){
  98. if(v==checkBtn){
  99. netText.setText(isNetConnected()+"");
  100. wifiText.setText(isWifiConnected()+"");
  101. net3gText.setText(is3gConnected()+"");
  102. gpsText.setText(isGpsEnabled()+"");
  103. }
  104. }
  105. }


布局xml:

[html] view plain copy
  1. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:orientation="vertical">
  5. <Button
  6. android:id="@+id/main_btn_check"
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content"
  9. android:text="检测网络"/>
  10. <LinearLayout
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content">
  13. <TextView
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:text="网络是否连接:"/>
  17. <EditText
  18. android:id="@+id/main_et_net"
  19. android:layout_width="match_parent"
  20. android:layout_height="wrap_content"
  21. android:layout_marginLeft="10dp"
  22. android:enabled="false"/>
  23. </LinearLayout>
  24. <LinearLayout
  25. android:layout_width="match_parent"
  26. android:layout_height="wrap_content">
  27. <TextView
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:text="wifi是否连接:"/>
  31. <EditText
  32. android:id="@+id/main_et_wifi"
  33. android:layout_width="match_parent"
  34. android:layout_height="wrap_content"
  35. android:layout_marginLeft="10dp"
  36. android:enabled="false"/>
  37. </LinearLayout>
  38. <LinearLayout
  39. android:layout_width="match_parent"
  40. android:layout_height="wrap_content">
  41. <TextView
  42. android:layout_width="wrap_content"
  43. android:layout_height="wrap_content"
  44. android:text="3G是否连接:"/>
  45. <EditText
  46. android:id="@+id/main_et_3g"
  47. android:layout_width="match_parent"
  48. android:layout_height="wrap_content"
  49. android:layout_marginLeft="10dp"
  50. android:enabled="false"/>
  51. </LinearLayout>
  52. <LinearLayout
  53. android:layout_width="match_parent"
  54. android:layout_height="wrap_content">
  55. <TextView
  56. android:layout_width="wrap_content"
  57. android:layout_height="wrap_content"
  58. android:text="GPS是否打开:"/>
  59. <EditText
  60. android:id="@+id/main_et_GPS"
  61. android:layout_width="match_parent"
  62. android:layout_height="wrap_content"
  63. android:layout_marginLeft="10dp"
  64. android:enabled="false"/>
  65. </LinearLayout>
  66. </LinearLayout>


记得加上相应权限~

附上图片结果:

android中检测网络连接状态简单总结_第1张图片

分享到:

更多相关文章

  1. Android之UI学习篇六:ImageView实现图片旋转和缩放
  2. 网络界面Android 网络连接--Wifi/3G
  3. 快速开发框架Afinal的使用(数据库操作,HTTP请求,网络图片加载,控件绑
  4. Android中的网络时间同步 !!!!!!!!
  5. Android网络编程之Http通信
  6. Android bitmap图片处理
  7. Android网络相关---上网流程
  8. Android 中,应用程序需要的图片资源如何针对不同屏幕大小手机设计

随机推荐

  1. Echarts(1):Python爬取微博热搜并用Echar
  2. Python连接SQLite数据库
  3. 鸿蒙系统的启动流程学习分享
  4. Python2.x与3.x版本有哪些主要的区别?
  5. Python操作Word
  6. Python3中的运算符
  7. Python3 环境搭建(Windows和Linux)
  8. JavaScript:1.图片懒加载演示;2.轮播图为
  9. 【数据库修复】.ReadInstrutions后缀勒索
  10. 【数据库修复】.ROGER后缀勒索病毒的数据