Code example for ConnectivityManager: Querying Network Reachability

Methods: getActiveNetworkInfo, requestRouteToHost

0
	} 
	 
	/* Return true if the appropriate host can be reached. */ 
	protected static boolean routeClear(String host, ConnectivityManager cm) {
		/* RequestRouteToHost apparently doesn't work over wifi: return true. */ 
		NetworkInfo ni = cm.getActiveNetworkInfo();
		if (ni.getType() == ConnectivityManager.TYPE_WIFI)
			return true; 
		 
		/* Do the DNS lookup using java.net library. */ 
		InetAddress a;
		try { 
			a = InetAddress.getByName(MENU_SERVER);
		} catch (UnknownHostException uhe) {
			Log.d(HTTP, uhe.toString());
			return false; 
		}	 
		/* Convert a byte array IP address into an integer representation. */ 
		byte[] b = a.getAddress();			 	// aaa.bbb.ccc.ddd
		int ipint = ((b[3] << 24) | 	// ipint bits[32-25] = aaa
					 (b[2] << 16) | 	// ipint bits[24-17] = bbb
					 (b[1] << 8 ) | 	// ipint bits[16- 9] = ccc
					  b[0]		   );	// ipint bits[ 8- 1] = ddd 
		 
		return cm.requestRouteToHost(ni.getType(), ipint);
	} 
	 
	/* Log Keys */ 
	public static final String HTTP 	= "HTTP Request";
	public static final String CACH	    = "File Input";