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 isMobileConnected(Context context) {
    ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    return (networkInfo != null && networkInfo.isConnected());
}

From source file:Main.java

public static boolean isWifiOnline(Context context) {
    ConnectivityManager conMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    State wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();

    if (wifi == NetworkInfo.State.CONNECTED)
        return true;
    else/*  w w  w  . j  ava 2 s  .c o m*/
        return false;
}

From source file:Main.java

public static boolean getMobileState(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    State wifistate = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
    if (wifistate != State.CONNECTED) {
        return false;
    }//from   w  w w.  j  a va 2s.c  om

    State mobileState = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
    boolean ret = State.CONNECTED == mobileState;
    return ret;
}

From source file:Main.java

public static boolean isWifi(Context context) {
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    return networkInfo.isConnected();
}

From source file:Main.java

public static boolean getWifiState(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    State wifistate = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
    if (wifistate != State.CONNECTED) {
        return false;
    }//from   w  w w . j  a  v  a2 s  .co  m

    State mobileState = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
    boolean ret = State.CONNECTED != mobileState;
    return ret;
}

From source file:Main.java

public static void reconnectWifi(Context context, int networkId) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    if (ni != null) {
        WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        if (networkId > -1) {
            wm.enableNetwork(networkId, true);
        }//from   ww w .  jav a 2  s  .  com
    }
}

From source file:Main.java

public static boolean isMobileOnline(Context context) {
    ConnectivityManager conMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    State mobile = conMan.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();

    if (mobile == NetworkInfo.State.CONNECTED)
        return true;
    else/*from   w w w.j a  v  a2  s  . c  o  m*/
        return false;
}

From source file:Main.java

public static int disconnectWifi(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    if (ni != null && ni.isConnectedOrConnecting()) {
        WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        int id = wm.getConnectionInfo().getNetworkId();
        return wm.disconnect() ? id : -1;
    }/* w w  w.  ja v a2  s  . co m*/

    return -1;
}

From source file:Main.java

public static boolean isUsingMobile(Context context) {
    ConnectivityManager cnm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni = cnm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    return ni != null && ni.isConnectedOrConnecting();
}

From source file:Main.java

public static boolean isConnectedToWifi(Context context) {
    ConnectivityManager connec = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo wifi = connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    if (wifi != null && wifi.isConnected()) {
        return true;
    } else {/* w  w  w . ja  v  a2 s  .c om*/
        return false;
    }

}