network: Android 网络判断


直接上代码,没有什么好说的!

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

另外还有两个方法判断网络是否可用:

  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.   
  13.     public static boolean isNetworkAvailable_01(Context context) {  
  14.         ConnectivityManager cm = (ConnectivityManager) context  
  15.                 .getSystemService(Context.CONNECTIVITY_SERVICE);  
  16.         NetworkInfo network = cm.getActiveNetworkInfo();  
  17.         if (network != null) {  
  18.             return network.isAvailable();  
  19.         }  
  20.         return false;  
  21.     }  
更加严谨的写法:
  1. public static boolean checkNet(Context context) {  
  2.           
  3.         try {  
  4.             ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);  
  5.             if (connectivity != null) {  
  6.               
  7.                 NetworkInfo info = connectivity.getActiveNetworkInfo();  
  8.                 if (info != null && info.isConnected()) {  
  9.                   
  10.                     if (info.getState() == NetworkInfo.State.CONNECTED) {  
  11.                         return true;  
  12.                     }  
  13.                 }  
  14.             }  
  15.         } catch (Exception e) {  
  16.         return false;  
  17. }  
  18.         return false;  
  19.     }  

相关内容