歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> network: Android 網絡判斷

network: Android 網絡判斷

日期:2017/3/1 10:28:29   编辑:Linux編程

直接上代碼,沒有什麼好說的!

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

另外還有兩個方法判斷網絡是否可用:

  1. public static boolean isNetworkAvailable_00(Context context) {
  2. ConnectivityManager cm = ((ConnectivityManager) context
  3. .getSystemService(Context.CONNECTIVITY_SERVICE));
  4. if (cm != null) {
  5. NetworkInfo info = cm.getActiveNetworkInfo();
  6. if (info != null && info.isConnectedOrConnecting()) {
  7. return true;
  8. }
  9. }
  10. return false;
  11. }
  12. public static boolean isNetworkAvailable_01(Context context) {
  13. ConnectivityManager cm = (ConnectivityManager) context
  14. .getSystemService(Context.CONNECTIVITY_SERVICE);
  15. NetworkInfo network = cm.getActiveNetworkInfo();
  16. if (network != null) {
  17. return network.isAvailable();
  18. }
  19. return false;
  20. }
更加嚴謹的寫法:
  1. public static boolean checkNet(Context context) {
  2. try {
  3. ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  4. if (connectivity != null) {
  5. NetworkInfo info = connectivity.getActiveNetworkInfo();
  6. if (info != null && info.isConnected()) {
  7. if (info.getState() == NetworkInfo.State.CONNECTED) {
  8. return true;
  9. }
  10. }
  11. }
  12. } catch (Exception e) {
  13. return false;
  14. }
  15. return false;
  16. }
Copyright © Linux教程網 All Rights Reserved