Example usage for android.net ConnectivityManager getActiveNetworkInfo

List of usage examples for android.net ConnectivityManager getActiveNetworkInfo

Introduction

In this page you can find the example usage for android.net ConnectivityManager getActiveNetworkInfo.

Prototype

@Deprecated
@RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
@Nullable
public NetworkInfo getActiveNetworkInfo() 

Source Link

Document

Returns details about the currently active default data network.

Usage

From source file:Main.java

public static NetworkInfo getCurrentActiveNetwork(Context mContext) {
    try {//w w w.ja  va 2s. co m
        ConnectivityManager connectivity = (ConnectivityManager) mContext
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            return connectivity.getActiveNetworkInfo();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static boolean isNetworkActivate(Context context) {

    boolean isConnected = false;
    ConnectivityManager cgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cgr == null) {
        return false;
    }/*from  w w w . j  av a  2  s.c om*/
    NetworkInfo info = cgr.getActiveNetworkInfo();
    if ((info != null) && info.isConnected()) {
        isConnected = true;
    }
    return isConnected;
}

From source file:Main.java

public static boolean isNetworkAvailable(Context context) {
    ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity != null) {
        NetworkInfo info = connectivity.getActiveNetworkInfo();
        if (info == null) {
            return false;
        } else {//w  w w  . j a  v  a2 s. c o m
            return true;
        }
    }
    return false;
}

From source file:Main.java

public static boolean isOnline(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm == null) {
        return false;
    }//from ww w  .j  a  v a2 s. c o  m
    NetworkInfo info = cm.getActiveNetworkInfo();
    return info != null && info.isConnectedOrConnecting();
}

From source file:Main.java

public static boolean isNetworkAvailable(Context context) {
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = null;/*from w ww  .  j  av  a  2  s .c  om*/
    if (manager != null)
        networkInfo = manager.getActiveNetworkInfo();
    if (networkInfo == null)
        return false;
    if (networkInfo.isConnected())
        return true;
    return false;
}

From source file:Main.java

public static int getActiveNetworkType(Context context) {
    int defaultValue = -1;
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm == null) {
        return defaultValue;
    }// w  ww .  j  av a  2  s  .  c  o m
    NetworkInfo info = cm.getActiveNetworkInfo();
    if (info == null) {
        return defaultValue;
    }
    return info.getType();
}

From source file:Main.java

/**
 * checks if there is WI-FI connection available
 * @param currentActivity//w  w w .j a v  a 2s  . c om
 * @return
 */
public static boolean isWifiInternetAvailable(Context currentActivity) {
    final ConnectivityManager connectivityManager = (ConnectivityManager) currentActivity
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    if (networkInfo != null) {
        if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
            if (networkInfo.isConnected()) {
                return true;
            }
        }
    }

    return false;
}

From source file:Main.java

public static boolean isConnected(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    if (null == cm) {
        return false;
    }/*w ww .  j av a2 s. c  om*/

    NetworkInfo info = cm.getActiveNetworkInfo();
    if (null != info && info.isConnected()) {
        if (info.getState() == NetworkInfo.State.CONNECTED) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

/**
 * Checks if we are online. Note that an internet connection might be reported,
 * but but that does not necessarily mean it's usable (eg. VPN, no DNS, ...)
 *///w  ww . j a va  2  s. c  o  m
public static boolean hasConnection(Context applicationContext) {

    ConnectivityManager connectivityManager = (ConnectivityManager) applicationContext
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    if (activeNetworkInfo == null || !activeNetworkInfo.isConnectedOrConnecting())
        return false;
    return true;
}

From source file:Main.java

public static String getNetworkType(Context context) {
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (manager == null)
        return "NO CONNECTIVITY";
    NetworkInfo netInfo = manager.getActiveNetworkInfo();
    if (netInfo == null)
        return "NO ACTIVE NETWORK";
    return netInfo.getTypeName().toLowerCase();
}