Android工具类-关于网络、状态的工具类

时间:2023-03-09 04:39:35
Android工具类-关于网络、状态的工具类

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

  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");