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

@SuppressLint("DefaultLocale")
public static String getAPNTypeString(Context context) {
    String netType = "";
    ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();

    if (networkInfo != null) {
        int nType = networkInfo.getType();
        if (nType == ConnectivityManager.TYPE_MOBILE) {
            String eInfo = networkInfo.getExtraInfo();
            if (eInfo != null) {
                netType = eInfo.toLowerCase();

            }//from   ww w.  j av a  2s . c  o  m
        } else if (nType == ConnectivityManager.TYPE_WIFI) {
            netType = "wifi";
        }
    }

    return netType;
}

From source file:Main.java

/**
 * check if network is available/*  www . jav a  2s  . c  om*/
 * @return
 * @param context
 */
public static boolean networkAvailable(Context context) {
    // get network manager
    ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    // get network info
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    // check network status
    if (networkInfo != null && networkInfo.isConnected()) {
        // fetch data
        return true;
    } else {
        // display error
        return false;
    }
}

From source file:Main.java

public static boolean checkNetworkConnectivity(Context context) {
    // get the system ConnectivityManager
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    if (networkInfo != null && (networkInfo.isAvailable() || networkInfo.isConnectedOrConnecting())) {
        if (networkInfo.getState() == NetworkInfo.State.CONNECTED) {
            return true;
        }/*w w  w . ja va2s.  co m*/
        // record the fact that there is live connection
    }
    return false;

}

From source file:Main.java

/**
 * checkout network connect/*ww w . j a v a 2  s .com*/
 *
 * @param context
 * @return
 */
public static boolean isNetworkConnected(Context context) {
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = manager.getActiveNetworkInfo();
    if (networkInfo != null) {
        return networkInfo.isAvailable();
    }
    return false;
}

From source file:Main.java

/**
 * method to check for network availability. returns true for available and
 * false for unavailable//from www . ja va 2s .com
 */
public static boolean isConnectionAvailable(Context context) {
    ConnectivityManager conn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo wifiNetwork = conn.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    NetworkInfo mobileNetwork = conn.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (wifiNetwork != null && wifiNetwork.isAvailable() == true
            && wifiNetwork.isConnectedOrConnecting() == true) {
        return true;
    } else if (mobileNetwork != null && mobileNetwork.isAvailable() == true
            && mobileNetwork.isConnectedOrConnecting() == true) {
        return true;
    } else
        return false;
}

From source file:Main.java

/**
 * is2G/*from  w  w  w.j  a va 2 s.c o m*/
 * @param context
 * @return boolean
 */
public static boolean is2G(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
    if (activeNetInfo != null && (activeNetInfo.getSubtype() == TelephonyManager.NETWORK_TYPE_EDGE
            || activeNetInfo.getSubtype() == TelephonyManager.NETWORK_TYPE_GPRS
            || activeNetInfo.getSubtype() == TelephonyManager.NETWORK_TYPE_CDMA)) {
        return true;
    }
    return false;
}

From source file:Main.java

public static boolean isNetworkAvailable(Activity activity) {
    Context context = activity.getApplicationContext();
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    if (connectivityManager == null) {
        return false;
    } else {//from w ww  .jav a2s . c  o  m
        NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();

        if (networkInfo != null && networkInfo.length > 0) {
            for (int i = 0; i < networkInfo.length; i++) {

                if (networkInfo[i].getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
    }
    return false;
}

From source file:Main.java

public static boolean isWifiConnected(Context context) {
    if (context == null) {
        return false;
    }//w w w .j a  v  a2  s .co m
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
    return (activeNetInfo != null) && (activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI);
}

From source file:Main.java

/**
 * Checks if a network is currently enabled on the device
 * //from  w w  w.  j  av  a 2  s .c om
 * @param ctx
 * @return
 */
public static boolean isNetworkAvailable(Context ctx) {
    ConnectivityManager connectivityManager = (ConnectivityManager) ctx
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

From source file:Main.java

public static boolean isNetworkConnected(Context context) {
    if (context == null) {
        return false;
    }//ww w  .j  a  va2  s.com
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
    return (activeNetInfo != null) && (activeNetInfo.isConnected());
}