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

/**
 * Checks if is online./*  ww  w  .  j a  va2  s  . c  o m*/
 *
 * @return true, if is online
 */
public static boolean isOnline(Context c) {
    ConnectivityManager cm = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    }
    return false;
}

From source file:Main.java

public static boolean isNetworkAvailable(Context context) {
    try {//  w  ww. j a  v  a  2  s .c o  m
        ConnectivityManager connMgr = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED
                || connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
                        .getState() == NetworkInfo.State.CONNECTING) {
            return true;
        } else if (connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
                .getState() == NetworkInfo.State.CONNECTED
                || connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
                        .getState() == NetworkInfo.State.CONNECTING) {

            return true;
        } else
            return false;
    } catch (Exception e) {
        return false;
    }

}

From source file:Main.java

public static boolean isWifiConnected(Context context) {
    if (context != null) {
        ConnectivityManager connManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo wifiInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (wifiInfo != null) {
            return wifiInfo.isAvailable();
        }/*from  ww  w .jav  a  2s.c  o m*/
    }
    return false;
}

From source file:Main.java

public static boolean isNetworkAvailable(Context context) {
    try {/*  w  w  w .  j a v a  2s. c  o m*/
        ConnectivityManager connManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connManager.getActiveNetworkInfo() != null && connManager.getActiveNetworkInfo().isAvailable()
                && connManager.getActiveNetworkInfo().isConnected()) {
            return true;
        }
    } catch (Exception ex) {

        ex.printStackTrace();
        return false;
    }
    return false;
}

From source file:Main.java

public static boolean isWifiConnected(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo wifiNetworkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (wifiNetworkInfo.isConnected())
        return true;
    return false;
}

From source file:Main.java

public static boolean isWifi(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeInfo = connectivityManager.getActiveNetworkInfo();
    if (activeInfo != null && activeInfo.getType() == ConnectivityManager.TYPE_WIFI) {
        return true;
    }//from  w  w w . ja  v  a2s  .c o  m
    return true;
}

From source file:Main.java

public static boolean isConnect(Context context) {

    ConnectivityManager mConnectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    TelephonyManager mTelephony = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE);
    // check if connect
    NetworkInfo netInfo = mConnectivity.getActiveNetworkInfo();
    if (netInfo == null || !mConnectivity.getBackgroundDataSetting()) {
        return false;
    } else {//from  w ww . java  2 s  . c o  m
        return true;
    }
}

From source file:Main.java

/***
 * /*from   www .ja  va2s .c o m*/
 * @param context
 * @return
 */
public static boolean isNetworkAvailable(Context context) {
    final ConnectivityManager connectivityManager = ((ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE));
    return connectivityManager.getActiveNetworkInfo() != null
            && connectivityManager.getActiveNetworkInfo().isConnected();
}

From source file:Main.java

public static boolean isMobileConnected(Context context) {
    if (context != null) {
        ConnectivityManager connManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mobileInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (mobileInfo != null) {
            return mobileInfo.isAvailable();
        }/*ww w . j  a  v a2 s. com*/
    }
    return false;
}

From source file:Main.java

public static boolean isNetworkAvailable(Context mContext) {
    boolean result = false;
    try {/*w  w w .j a v a 2 s .c  om*/
        ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);

        if (cm != null) {
            NetworkInfo ni = cm.getActiveNetworkInfo();
            if (ni != null) {
                result = ni.isAvailable();
            }
        }
    } catch (Exception e) {
    }

    return result;

}