Example usage for android.net ConnectivityManager TYPE_WIFI

List of usage examples for android.net ConnectivityManager TYPE_WIFI

Introduction

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

Prototype

int TYPE_WIFI

To view the source code for android.net ConnectivityManager TYPE_WIFI.

Click Source Link

Document

A WIFI data connection.

Usage

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 . java 2 s . 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 checkWifiNetwork(Context context) {
    boolean has = false;
    ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = connectivity.getActiveNetworkInfo();
    int netType = info.getType();
    int netSubtype = info.getSubtype();
    if (netType == ConnectivityManager.TYPE_WIFI) {
        has = info.isConnected();//from www .j av  a2 s  .  c o  m
    }
    return has;
}

From source file:Main.java

public static String getCurrentSsid(Context context) {
    String ssid = null;//from   www  .  j  a  va2s .  c om
    ConnectivityManager connManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (networkInfo.isConnected()) {
        final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
        if (connectionInfo != null && connectionInfo.getSSID() != null
                && !connectionInfo.getSSID().equals("")) {
            ssid = connectionInfo.getSSID();
        }
    }
    return ssid == null ? ssid : ssid.replace("\"", "");
}

From source file:Main.java

@SuppressWarnings("deprecation")
public static boolean isWifiConnected(Context context) {
    if (context != null) {
        ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mWiFiNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (mWiFiNetworkInfo != null) {
            return mWiFiNetworkInfo.isAvailable();
        }//from w ww  .j  av  a  2  s  .  co m
    }
    return false;
}

From source file:Main.java

public static boolean isWifiConnected(Context context) {
    if (context != null) {
        ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
        if (mNetworkInfo != null) {
            if (mNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                return true;
            }/*  www  .  ja  v a  2 s  . co  m*/
        }
    }
    return false;
}

From source file:Main.java

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

    // Wifi//from  w w w  .  j ava2  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

public static boolean checkConnection(Context context, boolean onlyWiFi) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
    boolean isWiFi = activeNetwork != null && activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;
    if (!onlyWiFi) {
        return isConnected;
    } else {//from w ww  . ja va  2 s.  c  om
        return isConnected && isWiFi;
    }
}

From source file:Main.java

/**
 * Checks if the device is currently online, works for both wifi and mobile networks.
 *///from  ww  w  . ja va2 s  . c  o m
public static boolean isOnline(Context context) {
    if (context == null)
        return false;
    boolean state = false;
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo wifiNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (wifiNetwork != null)
        state = wifiNetwork.isConnectedOrConnecting();
    NetworkInfo mobileNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (mobileNetwork != null)
        state = mobileNetwork.isConnectedOrConnecting();
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (activeNetwork != null)
        state = activeNetwork.isConnectedOrConnecting();
    return state;
}

From source file:Main.java

public static boolean hasConnection(final Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo wifiInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (wifiInfo != null && wifiInfo.isConnected()) {
        return true;
    }// w w  w  .ja v a2 s . c  o m
    wifiInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (wifiInfo != null && wifiInfo.isConnected()) {
        return true;
    }
    wifiInfo = cm.getActiveNetworkInfo();
    if (wifiInfo != null && wifiInfo.isConnected()) {
        return true;
    }
    return false;
}

From source file:Main.java

public static int getNetworkType(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    State mobileState = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
    State wifiState = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
    if (wifiState == State.CONNECTED || wifiState == State.CONNECTING) {
        return 1;
    } else if (mobileState == State.CONNECTED || mobileState == State.CONNECTING) {
        return 2;
    } else {//  ww w .  j  av a 2 s .c o m
        return 0;
    }
}