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

/**
 * Check for wifi internet connectivity/*  w w w  .jav  a  2  s. c o m*/
 *
 * @param context context
 * @return hasInternet boolean
 */
public static boolean hasWifiInternetAccess(@NonNull Context context) {
    try {
        boolean hasWifiInternet = false;
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (wifi != null && wifi.isAvailable() && networkInfo != null
                && networkInfo.isConnectedOrConnecting()) {

            hasWifiInternet = true;
        }

        return hasWifiInternet;
    } catch (Exception e) {
        return false;
    }
}

From source file:Main.java

public static boolean isWifiActive(Context c) { // Check if we are connected to the network via WiFi
    ConnectivityManager connManager = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo wifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (wifi == null) {
        return false;
    }/*from  ww w  .j av a  2  s .c o m*/
    if (wifi.isConnected()) {
        return true;
    }
    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 mWiFiNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (mWiFiNetworkInfo != null) {
            return mWiFiNetworkInfo.isAvailable();
        }/*from   www.  j a  v a 2s  .  c  o m*/
    }
    return false;
}

From source file:Main.java

public static boolean isWifiAvailable(final Context context) {

    if (context != null) {
        final ConnectivityManager connManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo[] connections = connManager.getAllNetworkInfo();
        for (NetworkInfo netInfo : connections) {
            if (netInfo != null) {
                boolean connected = netInfo.isConnected();
                int connectType = netInfo.getType();

                if (connected)
                    if (connectType == ConnectivityManager.TYPE_WIFI
                            || connectType == ConnectivityManager.TYPE_ETHERNET)
                        return true;
            }//from w w w .  j a v  a 2  s.  com
        }
    }
    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;
    }//  w  w  w .j  av a 2s  . c o 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:Main.java

/**
 * Checks if Network connectivity is available to download OpenCellID data
 * Requires:        android:name="android.permission.ACCESS_NETWORK_STATE"
 *//*from ww w . j a  v  a  2  s  .  c  om*/
public static Boolean isNetAvailable(Context context) {

    try {
        ConnectivityManager cM = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo wifiInfo = cM.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        NetworkInfo mobileInfo = cM.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (wifiInfo != null && mobileInfo != null) {
            return wifiInfo.isConnected() || mobileInfo.isConnected();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

From source file:Main.java

static public synchronized boolean checkConnectivity(Context context) {

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    // mMobile = prefs.getBoolean(ENUMPrefs.ENUM_PREF_MOBILE, false);
    mOnline = false;//from  w w w.j  av a  2  s.  c om

    ConnectivityManager mCmgr = (ConnectivityManager) context.getSystemService(Service.CONNECTIVITY_SERVICE);

    NetworkInfo ni = mCmgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (ni != null && ni.isConnected()) { /* wifi is on */
        mOnline = true;
    } else {
        if (mMobile) {
            /* if mobile is active and EDGE or better, we're good */
            ni = mCmgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            if (ni != null && ni.isConnected()) {
                mOnline = (ni.getSubtype() >= TelephonyManager.NETWORK_TYPE_EDGE);
            }
        }
    }
    return mOnline;
}

From source file:Main.java

public static boolean isWifiConnected(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    return cm != null && cm.getActiveNetworkInfo() != null
            && cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI;
}

From source file:com.owncloud.android.utils.ConnectivityUtils.java

public static boolean isAppConnectedViaUnmeteredWiFi(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    boolean result = cm != null && cm.getActiveNetworkInfo() != null
            && cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI
            && cm.getActiveNetworkInfo().getState() == NetworkInfo.State.CONNECTED
            && !ConnectivityManagerCompat.isActiveNetworkMetered(cm);
    Log_OC.d(TAG, "is AppConnectedViaWifi returns " + result);
    return result;
}

From source file:Main.java

/**
 * Used to determine if there is an active data connection and what type of
 * connection it is if there is one//from  w ww .  jav a  2s  . c  o  m
 * 
 * @param context The {@link Context} to use
 * @return True if there is an active data connection, false otherwise.
 *         Also, if the user has checked to only download via Wi-Fi in the
 *         settings, the mobile data and other network connections aren't
 *         returned at all
 */
public static final boolean isOnline(final Context context) {
    /*
     * This sort of handles a sudden configuration change, but I think it
     * should be dealt with in a more professional way.
     */
    if (context == null) {
        return false;
    }

    boolean state = false;
    final boolean onlyOnWifi = false;// PreferenceUtils.getInstace(context).onlyOnWifi();

    /* Monitor network connections */
    final ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    /* Wi-Fi connection */
    final NetworkInfo wifiNetwork = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (wifiNetwork != null) {
        state = wifiNetwork.isConnectedOrConnecting();
    }

    /* Mobile data connection */
    final NetworkInfo mbobileNetwork = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (mbobileNetwork != null) {
        if (!onlyOnWifi) {
            state = mbobileNetwork.isConnectedOrConnecting();
        }
    }

    /* Other networks */
    final NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
    if (activeNetwork != null) {
        if (!onlyOnWifi) {
            state = activeNetwork.isConnectedOrConnecting();
        }
    }

    return state;
}