Example usage for android.net NetworkInfo isConnected

List of usage examples for android.net NetworkInfo isConnected

Introduction

In this page you can find the example usage for android.net NetworkInfo isConnected.

Prototype

@Deprecated
public boolean isConnected() 

Source Link

Document

Indicates whether network connectivity exists and it is possible to establish connections and pass data.

Usage

From source file:Main.java

public static boolean haveInternet(Context context) {
    NetworkInfo info = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE))
            .getActiveNetworkInfo();//from w w  w .j a va  2  s. c om
    if (info == null || !info.isConnected()) {
        return false;
    }
    return true;
}

From source file:Main.java

static public String getMacAddress(Context context, ConnectivityManager connMananger) {
    Log.d(TAG, "getMacAddress");

    String macAddress = "";

    NetworkInfo info = connMananger.getActiveNetworkInfo();
    if (info != null && info.isConnected()) {
        int type = info.getType();

        Log.d(TAG, "connected type = " + type + " name " + info.getTypeName());

        if (type == ConnectivityManager.TYPE_WIFI) {
            WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            macAddress = wifiInfo.getMacAddress();
        } else if (type == ConnectivityManager.TYPE_ETHERNET || type == ConnectivityManager.TYPE_MOBILE) {
            String addressFileName = "/sys/class/net/eth0/address";
            File addressFile = new File(addressFileName);
            Log.d(TAG, "1");

            if (addressFile.exists()) {
                Log.d(TAG, "2");
                macAddress = readString(addressFile);
                Log.d(TAG, macAddress);//w w  w  .  j  ava2s .c  om
            } else {
                addressFileName = "/sys/class/net/eth1/address";
                addressFile = new File(addressFileName);
                if (addressFile.exists()) {
                    macAddress = readString(addressFile);
                    Log.d(TAG, macAddress);
                }
            }
        } else {
            //other type;
        }
    }
    return macAddress;
}

From source file:Main.java

public static boolean isNetworkAvailable(Context ctx) {
    ConnectivityManager cm = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo[] netInfo = cm.getAllNetworkInfo();

    for (NetworkInfo ni : netInfo) {
        String name = ni.getTypeName();

        if (name.equalsIgnoreCase("ETH"))
            if (ni.isConnected())
                return true;

        if (name.equalsIgnoreCase("WIFI"))
            if (ni.isConnected())
                return true;

        if (name.equalsIgnoreCase("MOBILE"))
            if (ni.isConnected())
                return true;
    }//ww w .j a  va 2 s . com

    return false;
}

From source file:Main.java

public static boolean networkUnreachable(Context cxt) {
    ConnectivityManager connectivityManager = (ConnectivityManager) cxt
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    if (null == activeNetworkInfo) {
        return true;
    }//  w w  w .  j a  v  a2  s.co  m
    return !activeNetworkInfo.isConnected();
}

From source file:Main.java

public static boolean isWiFiEnabled(Context context) {

    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    // i assume that connectivityManager cannot be null by default - because system service exists always \
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

    // simplified style of writing - I like this \
    return networkInfo != null && networkInfo.isConnected()
            && networkInfo.getType() == ConnectivityManager.TYPE_WIFI;
}

From source file:Main.java

public static boolean isOnline(Context c) {
    NetworkInfo networkInfo = ((ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE))
            .getActiveNetworkInfo();//from  w  w w.  ja va 2 s . c  om
    return networkInfo != null && networkInfo.isConnected();
}

From source file:Main.java

/**
 * Checks if a internet connection is available. Note that the device can be connected
 * to a network but not have internet access
 *
 * Requires <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 * in the manifest//from   ww w.j a v a 2 s .c  om
 *
 * @param appContext
 * @return
 */
public static boolean isNetworkAvailable(Context appContext) {
    /**
    ConnectivityManager cm =
        (ConnectivityManager) appContext.getSystemService(Context.CONNECTIVITY_SERVICE);
            
    return cm.getActiveNetworkInfo() != null &&
        cm.getActiveNetworkInfo().isConnectedOrConnecting();
     */
    boolean outcome = false;

    if (appContext != null) {
        ConnectivityManager cm = (ConnectivityManager) appContext
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo[] networkInfos = cm.getAllNetworkInfo();
        for (NetworkInfo tempNetworkInfo : networkInfos) {
            // Can also check if the user is in roaming
            if (tempNetworkInfo.isConnected()) {
                outcome = true;
                break;
            }
        }
    }

    return outcome;
}

From source file:Main.java

public static boolean isNetworkAvailable(Context context) {
    try {/*from w ww .j a  v  a 2s .  co m*/
        //define object of ConnectivityManager
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        //define object of NetworkInfo and get detailed info about
        // the currently active default data network from ConnectivityManager
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();

        //check connection
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    } catch (Exception ex) {
        Log.e("Helper", "isNetworkAvailable: " + ex.getMessage());
        return false;
    }
}

From source file:Main.java

public static boolean isNetworkAvailable(Context context) {
    NetworkInfo activeNetworkInfo = ((ConnectivityManager) context.getSystemService("connectivity"))
            .getActiveNetworkInfo();// w w w .  ja  v  a2  s.  c o m
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

From source file:Main.java

@SuppressWarnings({ "deprecation" })
public static String getNetWorkType(Context context) {
    String mNetWorkType = null;/*from  ww w . j a  v  a2 s. c o  m*/
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (manager == null) {
        return "ConnectivityManager not found";
    }
    NetworkInfo networkInfo = manager.getActiveNetworkInfo();
    if (networkInfo != null && networkInfo.isConnected()) {
        String type = networkInfo.getTypeName();
        if (type.equalsIgnoreCase("WIFI")) {
            mNetWorkType = NETWORKTYPE_WIFI;
        } else if (type.equalsIgnoreCase("MOBILE")) {
            String proxyHost = android.net.Proxy.getDefaultHost();
            if (TextUtils.isEmpty(proxyHost)) {
                mNetWorkType = mobileNetworkType(context);
            } else {
                mNetWorkType = NETWORKTYPE_WAP;
            }
        }
    } else {
        mNetWorkType = NETWORKTYPE_INVALID;
    }
    return mNetWorkType;
}