Example usage for android.content Context CONNECTIVITY_SERVICE

List of usage examples for android.content Context CONNECTIVITY_SERVICE

Introduction

In this page you can find the example usage for android.content Context CONNECTIVITY_SERVICE.

Prototype

String CONNECTIVITY_SERVICE

To view the source code for android.content Context CONNECTIVITY_SERVICE.

Click Source Link

Document

Use with #getSystemService(String) to retrieve a android.net.ConnectivityManager for handling management of network connections.

Usage

From source file:Main.java

public static boolean isNetworkAvailable(Context context) {
    try {// w ww .jav  a 2  s .  c o m
        //define object of ConnectivityManager
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        //define object of NetworkInfo and get detailed info about
        // the currently active default data network from ConnectivityManager
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();

        //check connection
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    } catch (Exception ex) {
        Log.e("Helper", "isNetworkAvailable: " + ex.getMessage());
        return false;
    }
}

From source file:Main.java

public static boolean HaveNetworkConnection(Context ctx) {

    boolean HaveConnectedWifi = false;
    boolean HaveConnectedMobile = false;

    ConnectivityManager cm = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo[] netInfo = cm.getAllNetworkInfo();
    for (NetworkInfo ni : netInfo) {
        if (ni.getTypeName().equalsIgnoreCase("WIFI")) {
            if (ni.isConnected()) {
                HaveConnectedWifi = true;
            }//  w  w  w  .j ava2s .  co m
        }
        if (ni.getTypeName().equalsIgnoreCase("MOBILE")) {
            if (ni.isConnected()) {
                HaveConnectedMobile = true;
            }
        }
    }
    return HaveConnectedWifi || HaveConnectedMobile;
}

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  .  ja va  2  s  . c  o  m*/
    }
    return false;
}

From source file:Main.java

/**
 * test if there is some connection/*from  w ww . j  a v a2  s  .c  o m*/
 * @param ctx
 * @return
 */
public static boolean verifyConnectivity(Context ctx) {

    ConnectivityManager connectivityManager = (ConnectivityManager) ctx
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
    return netInfo != null && netInfo.isConnectedOrConnecting();
}

From source file:Main.java

public static String getNetworkState(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    String returnValue = "";
    if (null != activeNetwork) {
        if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
            returnValue = "wifi";
        else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
            returnValue = "mobile" + "_" + getNetworkType(context);
        else//from  www.jav  a2  s. c  om
            returnValue = "Unknown";
    } else
        returnValue = "Not connected";
    return returnValue;
}

From source file:Main.java

public static boolean isConnected(Context context) {
    ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (conMgr.getActiveNetworkInfo() != null && conMgr.getActiveNetworkInfo().isAvailable()
            && conMgr.getActiveNetworkInfo().isConnected())
        return true;

    return false;
}

From source file:Main.java

/**
 * Check if device currently is online.//from www  .  j av a 2  s.c o  m
 *
 * @param context application context.
 * @return true if device is online.
 */
public static boolean isOnline(Context context) {
    final NetworkInfo networkInfo = ((ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
    return networkInfo != null && networkInfo.isConnectedOrConnecting();
}

From source file:Main.java

public static boolean checkConnection(Activity a) {
    boolean hasConnectedWifi = false;
    boolean hasConnectedMobile = false;

    ConnectivityManager cm = (ConnectivityManager) a.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo[] netInfo = cm.getAllNetworkInfo();
    for (NetworkInfo ni : netInfo) {
        if (ni.getTypeName().equalsIgnoreCase("wifi"))
            if (ni.isConnected())
                hasConnectedWifi = true;
        if (ni.getTypeName().equalsIgnoreCase("mobile"))
            if (ni.isConnected())
                hasConnectedMobile = true;
    }/*w w  w.ja  va 2  s .  c o  m*/
    return hasConnectedWifi || hasConnectedMobile;
}

From source file:Main.java

public static NetworkInfo.State getCurrentNetworkState(Context context) {
    NetworkInfo networkInfo = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE))
            .getActiveNetworkInfo();//from  ww  w .j a v a2s  .c o m
    return networkInfo != null ? networkInfo.getState() : null;
}

From source file:Main.java

public static String checkNetType(Context context) {
    ConnectivityManager connectMgr = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = connectMgr.getActiveNetworkInfo();
    String type = "no_network";
    if (info == null) {
        return type;
    }/*from  w  w w  .  j av  a  2s. com*/
    if (info.getType() == ConnectivityManager.TYPE_WIFI) {
        type = "wifi";
    } else {
        switch (info.getSubtype()) {
        case TelephonyManager.NETWORK_TYPE_UNKNOWN:
            type = "UNKNOWN";
            break;
        case TelephonyManager.NETWORK_TYPE_GPRS:
            type = "GPRS";
            break;
        case TelephonyManager.NETWORK_TYPE_EDGE:
            type = "EDGE";
            break;
        case TelephonyManager.NETWORK_TYPE_UMTS:
            type = "UMTS";
            break;
        case TelephonyManager.NETWORK_TYPE_CDMA:
            type = "CDMA";
            break;
        case TelephonyManager.NETWORK_TYPE_EVDO_0:
            type = "EVDO_0";
            break;
        case TelephonyManager.NETWORK_TYPE_EVDO_A:
            type = "EVDO_A";
            break;
        case TelephonyManager.NETWORK_TYPE_1xRTT:
            type = "1xRTT";
            break;
        case TelephonyManager.NETWORK_TYPE_HSDPA:
            type = "HSDPA";
            break;
        case TelephonyManager.NETWORK_TYPE_HSUPA:
            type = "HSUPA";
            break;
        case TelephonyManager.NETWORK_TYPE_HSPA:
            type = "HSPA";
            break;
        case TelephonyManager.NETWORK_TYPE_IDEN:
            type = "IDEN";
            break;
        case TelephonyManager.NETWORK_TYPE_EVDO_B:
            type = "EVDO_B";
            break;
        case TelephonyManager.NETWORK_TYPE_LTE:
            type = "LTE";
            break;
        case TelephonyManager.NETWORK_TYPE_EHRPD:
            type = "EHRPD";
            break;
        case TelephonyManager.NETWORK_TYPE_HSPAP:
            type = "HSPAP";
            break;
        }
    }
    return type;
}