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 int getNetworkState(Context context) {
    ConnectivityManager connManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    // Wifi//from  w ww.ja v a2 s. c o m
    State state = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
    if (state == State.CONNECTED || state == State.CONNECTING) {
        return NETWORN_WIFI;
    }

    // 3G
    state = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
    if (state == State.CONNECTED || state == State.CONNECTING) {
        return NETWORN_MOBILE;
    }
    return NETWORN_NONE;
}

From source file:Main.java

/**
 * get mobile NetWork Type//from  w  w  w .ja v  a2  s  .  com
 * 
 * @param context
 * @return
 */
public static String getNetWorkType(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo workinfo = connectivityManager.getActiveNetworkInfo();
    if (workinfo != null) {
        if (workinfo.getType() == ConnectivityManager.TYPE_BLUETOOTH) {
            return "b";
        }
        if (workinfo.getType() == ConnectivityManager.TYPE_MOBILE) {
            return "m";
        }
        if (workinfo.getType() == ConnectivityManager.TYPE_WIFI) {
            return "w";
        }
    }
    return null;
}

From source file:Main.java

/**
 * Check if there connection/*from  w w w.  j  a  v a 2s  .c  o m*/
 * @param context
 * @return
 */
public static boolean thereConnection(Context context) {

    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivityManager == null) {
        return false;
    }

    NetworkInfo[] info = connectivityManager.getAllNetworkInfo();
    if (info != null) {
        for (int i = 0; i < info.length; i++) {
            if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                return true;
            }
        }
    }
    return false;
}

From source file:Main.java

public static boolean isWapNet(Context context) {
    ConnectivityManager conManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = conManager.getActiveNetworkInfo();
    if (info != null && info.isAvailable()) {
        if (info.getType() == 1) {
            return false;
        } else {/* www. j a  v a  2  s .  c om*/
            String currentAPN = info.getExtraInfo();
            return !TextUtils.isEmpty(currentAPN) && (currentAPN.equals("cmwap") || currentAPN.equals("uniwap")
                    || currentAPN.equals("3gwap"));
        }
    } else {
        return false;
    }
}

From source file:Main.java

/**
 * Checks whether the device is connected to a network
 *//*w w  w.j a v  a2  s  .  c  om*/
public static boolean hasInternet(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;
    }
    return hasConnectedWifi || hasConnectedMobile;
}

From source file:Main.java

/**
 * Checks the devices connectivity/* www  . j av a2  s . c  o  m*/
 * 
 * @return True if connection is available, false if not
 */
public static boolean isConnected(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    return networkInfo == null ? false : networkInfo.isAvailable();
}

From source file:Main.java

/**
 * Checks if the device is currently online, works for both wifi and mobile networks.
 *//*from  w w  w.jav a 2  s .c  o  m*/
public static boolean isOnline(Context context) {
    if (context == null)
        return false;
    boolean state = false;
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo wifiNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (wifiNetwork != null)
        state = wifiNetwork.isConnectedOrConnecting();
    NetworkInfo mobileNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (mobileNetwork != null)
        state = mobileNetwork.isConnectedOrConnecting();
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (activeNetwork != null)
        state = activeNetwork.isConnectedOrConnecting();
    return state;
}

From source file:Main.java

/**
 * Method to check if network is available.
 *
 * @param context the context/*from w w w.  ja v a2  s  . co  m*/
 * @return Flag indicating if Network is available or not
 */
public static boolean isOnline(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 isNetworkAvailable(Context context) {
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = manager.getActiveNetworkInfo();
    return (activeNetworkInfo != null && activeNetworkInfo.isConnected());
}