Example usage for android.net NetworkInfo isConnected

List of usage examples for android.net NetworkInfo isConnected

Introduction

In this page you can find the example usage for android.net NetworkInfo isConnected.

Prototype

@Deprecated
public boolean isConnected() 

Source Link

Document

Indicates whether network connectivity exists and it is possible to establish connections and pass data.

Usage

From source file:Main.java

static boolean isConnected(Context paramContext) {
    NetworkInfo localNetworkInfo = ((ConnectivityManager) paramContext.getSystemService("connectivity"))
            .getActiveNetworkInfo();/*from w w w .ja  v  a2  s .  co  m*/
    if ((localNetworkInfo != null) && (localNetworkInfo.isConnected())) {
    }
    for (boolean bool = true;; bool = false) {
        return bool;
    }
}

From source file:Main.java

public static boolean isWifi(Context context) {
    NetworkInfo networkInfo = ((ConnectivityManager) (context.getSystemService(Context.CONNECTIVITY_SERVICE)))
            .getActiveNetworkInfo();//from ww  w  .  j  a v a 2  s. c  o  m
    return networkInfo != null && networkInfo.isConnected()
            && (networkInfo.getType() == ConnectivityManager.TYPE_WIFI
                    || networkInfo.getType() == ConnectivityManager.TYPE_ETHERNET || networkInfo.getType() == 17
                    || networkInfo.getType() == -1 || networkInfo.getType() == 13
                    || networkInfo.getType() == 16);
}

From source file:Main.java

public static boolean isWifiAvailable(Context context) {
    ConnectivityManager mConnMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo mWifi = mConnMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    boolean flag = false;
    if ((mWifi != null) && ((mWifi.isAvailable()))) {
        if ((mWifi.isConnected())) {
            flag = true;//  ww  w. jav  a 2s  .c o  m
        }
    }
    return flag;
}

From source file:Main.java

public static boolean isNetworkPresent(Context context) {
    boolean isNetworkAvailable = false;
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    try {//from w  w  w  . j a  v  a2  s .  c  o  m

        if (cm != null) {
            NetworkInfo netInfo = cm.getActiveNetworkInfo();
            if (netInfo != null) {

                isNetworkAvailable = netInfo.isConnected();
                //                    Toast.makeText(context, "Connecting...", Toast.LENGTH_SHORT).show();
                //Log.d("NETWORK<<","Connecting...."+netInfo.getReason());
            }
        }
    } catch (Exception ex) {
        //Log.e("Network Avail Error", ex.getMessage());

    }
    //        check for wifi also
    if (!isNetworkAvailable) {

        WifiManager connec = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

        State wifi = cm.getNetworkInfo(1).getState();
        if (connec.isWifiEnabled() && wifi.toString().equalsIgnoreCase("CONNECTED")) {
            isNetworkAvailable = true;
            //Log.d("WIFI NETWORK<<","WIFI connected");
        } else {

            isNetworkAvailable = false;
            // Log.d("WIFI Network<<","WIFI not connected");
        }

    }
    return isNetworkAvailable;

}

From source file:Main.java

public static boolean isNetworkAvailable(Context context) {
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = null;
    if (manager != null)
        networkInfo = manager.getActiveNetworkInfo();
    if (networkInfo == null)
        return false;
    if (networkInfo.isConnected())
        return true;
    return false;
}

From source file:Main.java

public static boolean isNetworkConnected(Context context) {
    try {//from   w w w . j a v a  2  s. c om
        NetworkInfo networkInfo = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE))
                .getActiveNetworkInfo();
        return networkInfo != null && networkInfo.isConnected();
    } catch (NullPointerException e) {
        e.printStackTrace();
        return false;
    }
}

From source file:Main.java

public static int getNetType(Context context) {
    int netWorkType = NETWORK_CLASS_UNKNOWN;

    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

    if (networkInfo != null && networkInfo.isConnected()) {
        int type = networkInfo.getType();

        if (type == ConnectivityManager.TYPE_WIFI) {
            netWorkType = NETWORK_WIFI;//from   w  ww.  j av a2s .  c o  m
        } else if (type == ConnectivityManager.TYPE_MOBILE) {
            TelephonyManager telephonyManager = (TelephonyManager) context
                    .getSystemService(Context.TELEPHONY_SERVICE);
            switch (telephonyManager.getNetworkType()) {
            case TelephonyManager.NETWORK_TYPE_GPRS:
            case TelephonyManager.NETWORK_TYPE_EDGE:
            case TelephonyManager.NETWORK_TYPE_CDMA:
            case TelephonyManager.NETWORK_TYPE_1xRTT:
            case TelephonyManager.NETWORK_TYPE_IDEN:
                return NETWORK_CLASS_2_G;
            case TelephonyManager.NETWORK_TYPE_UMTS:
            case TelephonyManager.NETWORK_TYPE_EVDO_0:
            case TelephonyManager.NETWORK_TYPE_EVDO_A:
            case TelephonyManager.NETWORK_TYPE_HSDPA:
            case TelephonyManager.NETWORK_TYPE_HSUPA:
            case TelephonyManager.NETWORK_TYPE_HSPA:
            case TelephonyManager.NETWORK_TYPE_EVDO_B:
            case TelephonyManager.NETWORK_TYPE_EHRPD:
            case TelephonyManager.NETWORK_TYPE_HSPAP:
                return NETWORK_CLASS_3_G;

            case TelephonyManager.NETWORK_TYPE_LTE:
                return NETWORK_CLASS_4_G;
            default:
                return NETWORK_CLASS_UNKNOWN;
            }
        }
    }

    return netWorkType;
}

From source file:Main.java

public static boolean isNetworkConnectedByWifi(Context context) {
    NetworkInfo networkInfo = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE))
            .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    return networkInfo != null && networkInfo.isConnected();
}

From source file:Main.java

public static boolean networkStatusOK(final Context context) {
    boolean netStatus = false;

    try {//from  www  .  j a  v  a2 s  . c  o  m
        ConnectivityManager connectManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectManager.getActiveNetworkInfo();
        if (activeNetworkInfo != null) {
            if (activeNetworkInfo.isAvailable() && activeNetworkInfo.isConnected()) {
                netStatus = true;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return netStatus;
}

From source file:Main.java

public static boolean isNetConnected(Context context) {
    boolean ret = false;
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

    if (networkInfo == null) {
        return ret;
    }/*w  ww  .j  a va2 s. co  m*/
    ret = networkInfo.isAvailable() & networkInfo.isConnected();
    return ret;
}