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

private static NetworkInfo getCurrentActiveNetworkInfo(Context context) {

    ConnectivityManager connectivityManager = (ConnectivityManager) context

            .getSystemService(Context.CONNECTIVITY_SERVICE);

    if (connectivityManager != null)

        return connectivityManager.getActiveNetworkInfo();

    return null;//from ww w .j  a va  2  s . com

}

From source file:Main.java

public static boolean isConnectivity(Context context) {

    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    return ((connectivityManager.getActiveNetworkInfo() != null
            && connectivityManager.getActiveNetworkInfo().getState() == NetworkInfo.State.CONNECTED)
            || telephonyManager.getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS);
}

From source file:com.musevisions.android.SudokuSolver.HttpPostUtils.java

static public boolean isConnected(Activity parent) {
    ConnectivityManager cm = (ConnectivityManager) parent.getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    return activeNetwork != null && activeNetwork.getState() == NetworkInfo.State.CONNECTED;
}

From source file:com.frame.network.utils.NetworkUtil.java

/**
 * ??//  w w w .j a  v  a  2 s. co  m
 * @param context
 * @return true if the network is available, false otherwise
 */
public static boolean isNetworkAwailable(Context context) {
    if (context == null) {
        return false;
    }
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = cm.getActiveNetworkInfo();
    if (info == null) {
        return false;
    }
    return info.isAvailable();
}

From source file:com.frame.network.utils.NetworkUtil.java

/**
 * ??// www. jav  a2  s .co  m
 * @param context
 * @return
 */
public static boolean isNetworkConnected(Context context) {
    if (context == null) {
        return false;
    }
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = cm.getActiveNetworkInfo();
    if (info == null) {
        return false;
    }
    return info.isConnected();
}

From source file:mc.lib.network.NetworkHelper.java

public static boolean hasConnection(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni = cm.getActiveNetworkInfo();
    if (ni != null && ni.isAvailable() && ni.isConnected()) {
        if (testConnection(TEST_SERVER1) || testConnection(TEST_SERVER2) || testConnection(TEST_SERVER3)) {
            Log.i(LOGTAG, "Internet connection detected");
            return true;
        }/*from  w  w  w . j av a 2s .  c  o  m*/
    }

    Log.i(LOGTAG, "No internet connection detected");
    return false;
}

From source file:Main.java

public static String getNetworkConnType(Context context) {
    ConnectivityManager connectMgr = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (null == connectMgr) {
        return null;
    }/*from  w  w w.j a va 2  s. co  m*/

    NetworkInfo info = connectMgr.getActiveNetworkInfo();
    if (info == null || !info.isConnected()) {
        return null;
    }
    if (info.isRoaming()) {
        // here is the roaming option you can change it if you want to
        // disable internet while roaming, just return false
        return null;
    }

    NetworkInfo mobileNetworkInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (null == mobileNetworkInfo) {
        return null;
    }
    NetworkInfo.State mobile = mobileNetworkInfo.getState();
    if (mobile == NetworkInfo.State.CONNECTED || mobile == NetworkInfo.State.CONNECTING) {
        return NETWORK_CONN_MOBILE;
    }

    NetworkInfo wifiNetworkInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (null == wifiNetworkInfo) {
        return null;
    }
    NetworkInfo.State wifi = wifiNetworkInfo.getState();
    if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING) {
        return NETWORK_CONN_WIFI;
    }
    return null;
}

From source file:in.huohua.peterson.network.NetworkUtils.java

public static boolean isNetworkAvailable(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = cm.getActiveNetworkInfo();
    return (info != null && info.isConnected());
}

From source file:Main.java

public static boolean canNetworkUseful(Context context) {

    ConnectivityManager manager = (ConnectivityManager) context.getApplicationContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    if (manager == null) {
        return false;
    }/*ww w.  j  a v a  2 s.co  m*/

    NetworkInfo networkinfo = manager.getActiveNetworkInfo();
    if (networkinfo == null || !networkinfo.isAvailable()) {
        return false;
    }

    return true;
}

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;
}