关闭

android判断网络是否可用方法

标签: 安卓网络连接
75人阅读 评论(0) 收藏 举报
分类:

网络连接判断

/**
 * 网络
 * @author Administrator
 *
 */
public final class NetworkUtil {

	private NetworkUtil(){
		
	}
	/**
	 * 判断网络是否可用
	 */
	public static boolean isNetworkConnected(Context context) {  
		      if (context != null) {  
		          ConnectivityManager mConnectivityManager = (ConnectivityManager) context  
		                  .getSystemService(Context.CONNECTIVITY_SERVICE);  
		          NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();  
		          if (mNetworkInfo != null) {  
		             return mNetworkInfo.isAvailable()&&mNetworkInfo.isConnected();  
		          }  
		      }  
		     return false;  
		 }
	/**
	 * 判断wifi是否可用
	 * @param context
	 * @return 
	 */
	public static boolean isWifiConnected(Context context) {  
		      if (context != null) {  
		          ConnectivityManager mConnectivityManager = (ConnectivityManager) context  
		                  .getSystemService(Context.CONNECTIVITY_SERVICE);  
		          NetworkInfo mWiFiNetworkInfo = mConnectivityManager  
		                  .getNetworkInfo(ConnectivityManager.TYPE_WIFI);  
		          if (mWiFiNetworkInfo != null&&mWiFiNetworkInfo.isConnected()) {  
		              return true;  
		          }  
		    }  
		     return false;  
		 }
	
	/**
	 * 判断mobile网络是否可用
	 */
	public static boolean isMobileConnected(Context context) {  
	    if (context != null) {  
	        ConnectivityManager mConnectivityManager = (ConnectivityManager) context  
	                .getSystemService(Context.CONNECTIVITY_SERVICE);  
	        NetworkInfo mMobileNetworkInfo = mConnectivityManager  
	                .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);  
	        if (mMobileNetworkInfo != null&&mMobileNetworkInfo.isConnected()) {  
	            return true;  
	        }  
	    }  
	    return false;  
	}
	/**
	 * 获取当前网络连接的类型信息
	 */
	public static int getConnectedType(Context context) {  
	    if (context != null) {  
	        ConnectivityManager mConnectivityManager = (ConnectivityManager) context  
	                .getSystemService(Context.CONNECTIVITY_SERVICE);  
	        NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
	        if (mNetworkInfo != null && mNetworkInfo.isConnected()) {  
	            return mNetworkInfo.getType();  
	        }  
	    }  
	    return -1;  
	}
	
	/**
	 * 判断URL是否能连接成功
	 * 
	 * @param urlStr
	 * @return
	 */
	public static boolean isConnect(String urlStr) {
		boolean isConnected = false;
		if (urlStr == null || urlStr.length() <= 0) {
			return false;
		}
			try {
				URL url = new URL(urlStr);
				HttpURLConnection con = (HttpURLConnection) url
						.openConnection();
				con.setConnectTimeout(3000);
				int state = con.getResponseCode();
				if (state == 200) {
					isConnected = true;
				}
			} catch (Exception ex) {
				isConnected = false;
			}
		return isConnected;
	}
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:318次
    • 积分:60
    • 等级:
    • 排名:千里之外
    • 原创:6篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章存档