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 isNetworkAvailable(Context context) {
    boolean status = false;
    try {/*from  w  ww.ja v a 2 s.c  o  m*/
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getNetworkInfo(0);
        if (netInfo != null && netInfo.getState() == NetworkInfo.State.CONNECTED) {
            status = true;

        } else {
            netInfo = cm.getNetworkInfo(1);
            if (netInfo != null && netInfo.getState() == NetworkInfo.State.CONNECTED)
                status = true;
        }

    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }

    return status;
}

From source file:Main.java

public static boolean checkWifiConnection(Context context) {
    final ConnectivityManager connMgr = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (wifi.isAvailable())
        return true;
    else//  w ww.  j a  va 2 s.com
        return false;
}

From source file:Main.java

private static boolean isWifiConnected(Context context) {
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo info = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (info != null && info.isConnected()) {
        return true;
    }/*  ww  w  .ja v  a 2 s.  c  o m*/

    return false;

}

From source file:Main.java

public static Boolean NetWorkIsWifi(Context mContext) {
    boolean netStatus = false;
    ConnectivityManager connManager = (ConnectivityManager) mContext
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (mWifi.isConnected()) {
        netStatus = true;//from  www . j  ava  2  s .  c o  m

    }
    return netStatus;

}

From source file:Main.java

public static boolean checkNetworkConnection(Context context) {
    final ConnectivityManager connMgr = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    final NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (wifi.isAvailable() || mobile.isAvailable())
        return true;
    else//w ww  .ja  v  a  2 s  .  com
        return false;
}

From source file:Main.java

public static int availableCommunication(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    boolean isWifiAvail = ni.isAvailable();
    boolean isWifiConn = ni.isConnected();
    ni = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    boolean isMobileAvail = ni.isAvailable();
    boolean isMobileConn = ni.isConnected();
    String status = "WiFi\nAvail = " + isWifiAvail + "\nConn = " + isWifiConn + "\nMobile\nAvail = "
            + isMobileAvail + "\nConn = " + isMobileConn + "\n";
    Log.d("Communication State! ", status);
    if (isWifiAvail == true && isWifiConn == true) {
        return ConnectivityManager.TYPE_WIFI;
    } else if (isMobileAvail == true && isMobileConn == true) {
        return ConnectivityManager.TYPE_MOBILE;
    } else {/* w  w w  .  j  av a  2 s .  c o m*/
        return -1;
    }
}

From source file:Main.java

public static boolean isMobileOrWifiConneciton(Context context) {

    ConnectivityManager connManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    NetworkInfo mMobile = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

    if (mWifi.isAvailable() == false && mMobile.isAvailable() == false) {
        return false;
    }/*from   www  .  jav  a 2  s.  c  o m*/

    return true;
}

From source file:Main.java

public static boolean hasConnected(Context context) {
    ConnectivityManager connectMgr = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo mobNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    NetworkInfo wifiNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if ((mobNetInfo == null || !mobNetInfo.isConnected())
            && (wifiNetInfo == null || !wifiNetInfo.isConnected())) {
        return false;
    }/*  w  w  w.java2 s  .  co  m*/

    return true;
}

From source file:Main.java

public static boolean isWifiAvailable(Activity activity) {
    ConnectivityManager connManager = (ConnectivityManager) activity
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    return mWifi.isConnected();
}

From source file:Main.java

/**
 * This method is used to check whether Wi-Fi is connected or not.
 * @param appContext/*ww  w .  ja va 2  s.c o m*/
 * @return
 */
public static boolean isWiFiConnected(Context appContext) {
    boolean isConnected = false;
    ConnectivityManager connMgr = (ConnectivityManager) appContext
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    if (wifi != null && wifi.isAvailable()) {
        isConnected = true;
    }
    return isConnected;
}