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 checkNetworkConnection(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    State wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
    if (wifi == State.CONNECTED) {
        return true;
    }/*from   w  w w  . j av  a 2 s  . c o m*/
    State mobile = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
    if (mobile == State.CONNECTED) {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        int type = tm.getNetworkType();
        if (type != TelephonyManager.NETWORK_TYPE_CDMA && type != TelephonyManager.NETWORK_TYPE_EDGE
                && type != TelephonyManager.NETWORK_TYPE_GPRS) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

public static boolean isMobileConnected(Context context) {
    if (context != null) {
        ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mMobileNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (mMobileNetworkInfo != null) {
            return mMobileNetworkInfo.isAvailable();
        }/* w w  w .j a va2s  .  co m*/
    }
    return false;
}

From source file:Main.java

public static boolean isWifiConnected(Context context) {
    if (context != null) {
        ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
        if (mNetworkInfo != null) {
            if (mNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                return true;
            }/*from  w  w w .  j av a  2  s  . com*/
        }
    }
    return false;
}

From source file:Main.java

public static boolean isMobileConnected(Context context) {
    if (context != null) {
        ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mMobileNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (mMobileNetworkInfo != null) {
            return mMobileNetworkInfo.isConnected() && mMobileNetworkInfo.isAvailable();
        }//from  w  ww . j  ava  2  s  .  co m
    }
    return false;
}

From source file:Main.java

public static boolean checkNetworkState(Context context) {
    boolean netstate = false;
    ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity != null) {
        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info != null) {
            for (int i = 0; i < info.length; i++) {
                if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                    netstate = true;//from   w  w  w  .  ja v  a  2  s  .co m
                    break;
                }
            }
        }
    }
    return netstate;
}

From source file:Main.java

public static boolean isConnectingToInternet(Context context) {
    try {/*from w  w  w.ja  v a 2 s . co  m*/
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnected()) {
            return true;
        }
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return false;
}

From source file:Main.java

public static boolean CheckNetworkConnect(Context context) {
    boolean result = false;
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo mobileInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    NetworkInfo wifiInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    NetworkInfo ethInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
    NetworkInfo activeInfo = manager.getActiveNetworkInfo();
    Log.v("networkInfo", "mobile:" + mobileInfo.isConnected() + "\n" + "wifi:" + wifiInfo.isConnected() + "\n"
            + "eth:" + ethInfo.isConnected() + "\n" + "active:" + activeInfo.getTypeName());
    if (wifiInfo.isConnected() || ethInfo.isConnected()) {
        result = true;//from ww  w .  j  ava 2 s .c o  m
    }
    return result;
}

From source file:Main.java

public static boolean isWiFiActive(Context context) {
    ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity != null) {
        NetworkInfo[] 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;
                }/*from  www  . ja va 2s.  c o m*/
            }
        }
    }
    return false;
}

From source file:Main.java

public static int disconnectWifi(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    if (ni != null && ni.isConnectedOrConnecting()) {
        WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        int id = wm.getConnectionInfo().getNetworkId();
        return wm.disconnect() ? id : -1;
    }//w w  w. j  a  v  a  2 s. com

    return -1;
}

From source file:Main.java

public static boolean isMobileConnected(Context context) {
    if (context != null) {
        ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
        if (mNetworkInfo != null) {
            if (mNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                return true;
            }/*  ww w.  j  a  v a2 s  .c o m*/
        }
    }
    return false;
}