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 isConnAvailAndNotRoaming(Context context) {

    ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    if (conMgr.getActiveNetworkInfo() != null && conMgr.getActiveNetworkInfo().isAvailable()
            && conMgr.getActiveNetworkInfo().isConnected()) {

        if (!conMgr.getActiveNetworkInfo().isRoaming())
            return true;
        else//from   ww w.java 2 s . c  om
            return false;
    } else {
        Log.w(TAG, "Internet Connection NOT Present");
        return false;
    }
}

From source file:Main.java

public static boolean isMobileOnline(Context context) {
    ConnectivityManager conMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    State mobile = conMan.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();

    if (mobile == NetworkInfo.State.CONNECTED)
        return true;
    else//from   ww  w  .j a  va  2s .co  m
        return false;
}

From source file:Main.java

/** Returns true if device is connected to Internet, otherwise false. */
public static boolean hasInternetConnection(final Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivityManager.getActiveNetworkInfo() != null
            && connectivityManager.getActiveNetworkInfo().isAvailable()
            && connectivityManager.getActiveNetworkInfo().isConnected()) {
        return true;
    } else {/*  w w w  .jav a  2  s  .  c  o m*/
        return false;
    }
}

From source file:Main.java

public static boolean isNetworkAvailable(Context context) {
    try {/*from  w  w  w  .  j  a  v a2  s. c  o  m*/
        ConnectivityManager manager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = manager.getActiveNetworkInfo();
        return (info != null && info.isAvailable());
    } catch (Exception e) {
        // ignore
    }
    return false;
}

From source file:Main.java

public static void reconnectWifi(Context context, int networkId) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    if (ni != null) {
        WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        if (networkId > -1) {
            wm.enableNetwork(networkId, true);
        }//from ww  w.  ja v  a2  s  .c  om
    }
}

From source file:Main.java

public static boolean isNetworkAvailable() {
    Context c = getContext();/*from   w  w  w  . j a  v  a 2 s .  c  o m*/
    ConnectivityManager connectivityManager = (ConnectivityManager) c
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    boolean errorInNetwork = activeNetworkInfo != null && activeNetworkInfo.isConnected();
    return errorInNetwork;
}

From source file:Main.java

public static boolean isOnline(Context context) {
    boolean haveConnectedWifi = false;
    boolean haveConnectedMobile = false;
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo[] netInfo = cm.getAllNetworkInfo();
    for (NetworkInfo ni : netInfo) {
        if (ni.getTypeName().equalsIgnoreCase("WIFI"))
            if (ni.isConnected())
                haveConnectedWifi = true;
        if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
            if (ni.isConnected())
                haveConnectedMobile = true;
    }/*w  w  w . j  av  a 2s  .  c  o  m*/
    return haveConnectedWifi || haveConnectedMobile;
}

From source file:Main.java

public static NetworkInfo getNetworkInfo() {
    if (mAppContext == null)
        return null;
    ConnectivityManager cm = (ConnectivityManager) mAppContext.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni = cm.getActiveNetworkInfo();
    return ni;//ww w.java2  s.co m
}

From source file:Main.java

public static boolean isWifiActive(Context icontext) {
    Context context = icontext.getApplicationContext();
    ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo[] info;/*from  w w w.j av  a2 s. com*/
    if (connectivity != null) {
        info = connectivity.getAllNetworkInfo();
        if (info != null) {
            for (int i = 0; i < info.length; i++) {
                if (info[i].getTypeName().equals("WIFI") && info[i].isConnected()) {
                    return true;
                }
            }
        }
    }
    return false;
}

From source file:Main.java

public static int isNetworkAvailable(Context context) {
    ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity == null) {
        return 0;
    } else {/*from  w ww  .ja  va2 s . c  om*/
        NetworkInfo[] infos = connectivity.getAllNetworkInfo();
        if (infos != null) {
            for (NetworkInfo info : infos) {
                if (info.getState() == NetworkInfo.State.CONNECTED) {
                    if (info.getType() == ConnectivityManager.TYPE_WIFI) {
                        return 1;
                    } else if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
                        String extraInfo = info.getExtraInfo();
                        if ("cmwap".equalsIgnoreCase(extraInfo) || "cmwap:gsm".equalsIgnoreCase(extraInfo))
                            return 2;
                        return 3;
                    }
                }
            }
        }
    }
    return 0;
}