Is connected : Connectivity « Network « Android






Is connected

    

//package com.totsp.bookworm.util;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.util.Log;


import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

/**
 * NetworkUtils.
 * 
 * @author ccollins
 *
 */
public final class NetworkUtil {

   private NetworkUtil() {
   }

   public static String getIpAddress() {
      try {
         for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
               InetAddress inetAddress = enumIpAddr.nextElement();
               if (!inetAddress.isLoopbackAddress()) {
                  return inetAddress.getHostAddress().toString();
               }
            }
         }
      } catch (SocketException e) {
        // Log.e(Constants.LOG_TAG, e.getMessage(), e);
      }
      return null;
   }

   public static boolean connectionPresent(final ConnectivityManager cMgr) {
      if (cMgr != null) {
         NetworkInfo netInfo = cMgr.getActiveNetworkInfo();
         if ((netInfo != null) && (netInfo.getState() != null)) {
            return netInfo.getState().equals(State.CONNECTED);
         } else {
            return false;
         }
      }
      return false;
   }
}

   
    
    
    
  








Related examples in the same category

1.is Network Available
2.System InetAddress
3.is Net Connected
4.Connectivity Tester
5.Is Android connected to your server
6.Is Connected To Internet