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 isNetworkConnected(Context context) {
    if (context != null) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        if (networkInfo != null) {
            return networkInfo.isAvailable();
        }// w  w  w .ja va2s. c o m
    }
    return false;
}

From source file:Main.java

public static boolean isOnline(Context ctx) {
    try {/*from ww  w  . j  ava2 s .co  m*/
        ConnectivityManager cm = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
        return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnectedOrConnecting();

    } catch (Exception ex) {
        if (enableLogs)
            Log.e(TAG, ex.toString());
    }
    return false;
}

From source file:Main.java

public static int getCurrentNetworkSubtype(Context context) {
    NetworkInfo networkInfo = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE))
            .getActiveNetworkInfo();//  w w  w . j  a v a  2s.  co  m
    return networkInfo != null ? networkInfo.getSubtype() : -1231545315;
}

From source file:Main.java

@SuppressLint("DefaultLocale")
public static boolean isCMWap(Context context) {
    boolean res = false;
    ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();

    if (networkInfo == null) {
        return res;
    }//  w w w  .j  ava  2  s . c  om
    int nType = networkInfo.getType();
    String eInfo = networkInfo.getExtraInfo();
    if (nType == ConnectivityManager.TYPE_MOBILE && eInfo != null) {
        if (eInfo.toLowerCase().equals("cmwap")) {
            res = true;
        }
    }

    return res;
}

From source file:Main.java

public static final boolean hasConnectivity(final Context context, final int... networkTypes) {
    final ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (networkTypes.length == 0)
        for (final NetworkInfo info : connectivity.getAllNetworkInfo())
            if (info.isAvailable() || info.isConnected() || info.isConnectedOrConnecting())
                return true;
    for (final int networkType : networkTypes)
        if (ConnectivityManager.isNetworkTypeValid(networkType)) {
            final NetworkInfo info = connectivity.getNetworkInfo(networkType);
            if (info.isAvailable() || info.isConnected() || info.isConnectedOrConnecting())
                return true;
        }//from   w  w  w  . j a  v a 2 s  .c o  m
    return false;
}

From source file:Main.java

public static boolean isNetworkReachable(Context mContext) {
    boolean bRet = false;
    try {/*w  ww  . ja v a2s . com*/
        ConnectivityManager conn = (ConnectivityManager) mContext
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = conn.getActiveNetworkInfo();
        bRet = (null == netInfo) ? false : netInfo.isAvailable();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bRet;
}

From source file:Main.java

public static int getConnectivityStatus(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (null != activeNetwork) {
        if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
            return TYPE_WIFI;

        if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
            return TYPE_MOBILE;
    }/*from  ww  w  .ja  va  2  s. c om*/
    return TYPE_NOT_CONNECTED;
}

From source file:Main.java

public static boolean isConnected(Context context) {
    Log.d(TAG, "isConnected(context = " + context + ")");

    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();

    return netInfo != null && netInfo.isConnectedOrConnecting();
}

From source file:Main.java

public static boolean ensureWifi(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo == null || !netInfo.isConnectedOrConnecting())
        return false;
    // always OK if we're on wifi
    if (netInfo.getType() == ConnectivityManager.TYPE_WIFI)
        return true;
    // check for wifi only pref
    if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean("wifiPref", true)) {
        Log.d("Podax", "Not downloading because Wifi is required and not connected");
        return false;
    }//from  www. ja  v  a2 s  . com
    // check for 3g data turned off
    if (!netInfo.isConnected()) {
        Log.d("Podax", "Not downloading because background data is turned off");
        return false;
    }

    return true;
}

From source file:Main.java

public static boolean isNetworkAvailable(Context context) {
    if (context == null)
        return false;

    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}