Example usage for android.net ConnectivityManager getNetworkInfo

List of usage examples for android.net ConnectivityManager getNetworkInfo

Introduction

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

Prototype

@Deprecated
@RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
@Nullable
public NetworkInfo getNetworkInfo(@Nullable Network network) 

Source Link

Document

Returns connection status information about a particular Network.

Usage

From source file:Main.java

public static boolean isGPRSConnected(Context context) {
    ConnectivityManager cmg = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    boolean result = true;
    NetworkInfo active = cmg.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (active == null || !active.isConnected()) {
        result = false;//from  ww  w.  j a  v  a 2 s.  c o  m
    }
    return result;
}

From source file:Main.java

public static boolean isMobileConnected(Context context) {
    ConnectivityManager mgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (mgr == null)
        return false;
    NetworkInfo info = mgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (info == null)
        return false;
    NetworkInfo.State state = info.getState();
    return state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING;
}

From source file:Main.java

public static int getNetworkState(Context context) {
    ConnectivityManager connManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    // Wifi/*from  ww  w  . j  a v  a 2 s. c o  m*/
    State state = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
    if (state == State.CONNECTED || state == State.CONNECTING) {
        return NETWORN_WIFI;
    }

    // 3G
    state = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
    if (state == State.CONNECTED || state == State.CONNECTING) {
        return NETWORN_MOBILE;
    }
    return NETWORN_NONE;
}

From source file:Main.java

private static boolean isNetworkConnected(Context context, int typeMobile) {
    boolean ret = false;
    if (!isNetConnected(context)) {
        return ret;
    }//from   w w  w.  j a  v a2 s.c om
    ConnectivityManager connectManger = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectManger.getNetworkInfo(typeMobile);
    if (networkInfo == null) {
        return ret;
    }
    return ret;
}

From source file:Main.java

public static boolean acquireWifiLock(Context context) {
    //We only allow backup on wifi connection. Make sure we are connected and if so lock the wifi connection
    ConnectivityManager connManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo wifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (wifi.isConnected()) {
        if (wifiLock == null) {
            WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            wifiLock = wm.createWifiLock(WifiManager.WIFI_MODE_FULL, "com.zns.comicdroid.wifilock");
            wifiLock.setReferenceCounted(true);
            wifiLock.acquire();//from w  w  w . ja v  a  2 s  . c om
        } else {
            wifiLock.acquire();
        }
        return true;
    }
    return false;
}

From source file:Main.java

public static boolean isNetworkAvailable(Context context) {
    ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    boolean availability = false;
    try {/*  w w  w .ja v  a2s.c om*/
        if (conMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED
                || conMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
                        .getState() == NetworkInfo.State.CONNECTED) {
            availability = true;
        }
    } catch (Exception e) {
        return true;
    }
    return availability;
}

From source file:Main.java

public static boolean isWifiConnected(Context context) {
    ConnectivityManager connManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    // Wlan Network
    State wifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
    return (wifi == State.CONNECTED || wifi == State.CONNECTING);
}

From source file:Main.java

public static boolean isWifiConnected(Context context) {
    if (context != null) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo wiFiNetworkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (wiFiNetworkInfo != null) {
            return wiFiNetworkInfo.isAvailable();
        }/*  ww  w  .j  a v  a2s.  c  om*/
    }
    return false;
}

From source file:Main.java

public static boolean isMobileConnected(Context context) {
    if (context != null) {
        ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mMobileNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (mMobileNetworkInfo != null) {
            return mMobileNetworkInfo.isAvailable();
        }// w  ww .j  av  a2  s  .co m
    }
    return false;
}

From source file:Main.java

public static boolean isMobileConnected(Context context) {
    if (context != null) {
        ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mMobileNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (mMobileNetworkInfo != null) {
            return mMobileNetworkInfo.isConnected() && mMobileNetworkInfo.isAvailable();
        }//from  ww  w . j a  v a 2s.co m
    }
    return false;
}