欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android中判斷網(wǎng)絡(luò)連接是否可用的方法總結(jié)

 更新時(shí)間:2017年04月18日 10:17:31   投稿:lqh  
這篇文章主要介紹了Android中判斷網(wǎng)絡(luò)連接是否可用的方法總結(jié)的相關(guān)資料,需要的朋友可以參考下

Android 網(wǎng)路判斷

判斷當(dāng)前網(wǎng)絡(luò)是否是wifi

/**
   * 判斷當(dāng)前是否是wifi
   * @param mContext
   * @return
   */
  private static boolean isWifi(Context mContext) { 
    ConnectivityManager connectivityManager = (ConnectivityManager) mContext 
        .getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo(); 
    if (activeNetInfo != null 
        && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) { 
      return true; 
    } 
    return false; 
  } 
}

一、判斷網(wǎng)絡(luò)連接是否可用

public static boolean isNetworkAvailable(Context context) { 
 ConnectivityManager cm = (ConnectivityManager) context 
  .getSystemService(Context.CONNECTIVITY_SERVICE); 
 if (cm == null) { 
 } else {
       //如果僅僅是用來(lái)判斷網(wǎng)絡(luò)連接
       //則可以使用 cm.getActiveNetworkInfo().isAvailable(); 
  NetworkInfo[] info = cm.getAllNetworkInfo(); 
  if (info != null) { 
  for (int i = 0; i < info.length; i++) { 
   if (info[i].getState() == NetworkInfo.State.CONNECTED) { 
   return true; 
   } 
  } 
  } 
 } 
 return false; 
 } 

  二、判斷GPS是否打開(kāi)

 public static boolean isGpsEnabled(Context context) { 
 LocationManager lm = ((LocationManager) context 
  .getSystemService(Context.LOCATION_SERVICE)); 
 List<String> accessibleProviders = lm.getProviders(true); 
 return accessibleProviders != null && accessibleProviders.size() > 0; 
 } 

 三、判斷WIFI是否打開(kāi)

public static boolean isWifiEnabled(Context context) { 
 ConnectivityManager mgrConn = (ConnectivityManager) context 
  .getSystemService(Context.CONNECTIVITY_SERVICE); 
 TelephonyManager mgrTel = (TelephonyManager) context 
  .getSystemService(Context.TELEPHONY_SERVICE); 
 return ((mgrConn.getActiveNetworkInfo() != null && mgrConn 
  .getActiveNetworkInfo().getState() == NetworkInfo.State.CONNECTED) || mgrTel 
  .getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS); 
 } 

四、判斷是否是3G網(wǎng)絡(luò)

 public static boolean is3rd(Context context) { 
 ConnectivityManager cm = (ConnectivityManager) context 
  .getSystemService(Context.CONNECTIVITY_SERVICE); 
 NetworkInfo networkINfo = cm.getActiveNetworkInfo(); 
 if (networkINfo != null 
  && networkINfo.getType() == ConnectivityManager.TYPE_MOBILE) { 
  return true; 
 } 
 return false; 
 } 

五、判斷是wifi還是3g網(wǎng)絡(luò),用戶的體現(xiàn)性在這里了,wifi就可以建議下載或者在線播放。

public static boolean isWifi(Context context) { 
  ConnectivityManager cm = (ConnectivityManager) context 
   .getSystemService(Context.CONNECTIVITY_SERVICE); 
  NetworkInfo networkINfo = cm.getActiveNetworkInfo(); 
  if (networkINfo != null 
   && networkINfo.getType() == ConnectivityManager.TYPE_WIFI) { 
  return true; 
  } 
  return false; 
 }

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

最新評(píng)論