Code example for ConnectivityManager: Querying Network Reachability

Methods: getActiveNetworkInfogetNetworkInfo

0
        if (conMgr == null) {
            return; 
        } 
 
        // Check if an active network is available. 
        final NetworkInfo activeInfo = conMgr.getActiveNetworkInfo();
        if (activeInfo != null && activeInfo.isConnected()) {
            setHasNetwork(true, null); 
            return; 
        } 
 
        // Try WIFI data connection. 
        final NetworkInfo wifiInfo = conMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (wifiInfo != null && wifiInfo.isConnected()) {
            setHasNetwork(true, null); 
            return; 
        } 
 
        // Try mobile connection. 
        final NetworkInfo mobileInfo = conMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
	if (mobileInfo == null) {
            setHasNetwork(false, "No active data connection"); 
            return; 
        }