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:com.zirconi.huaxiaclient.SharedApplication.java

public static boolean networkIsAvailable(Context context) {
    ConnectivityManager cManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = cManager.getActiveNetworkInfo();
    if (info == null) {
        return false;
    }//from   w  ww .j av  a2  s.  co m
    if (info.isConnected()) {
        return true;
    }
    return false;
}

From source file:Main.java

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

        try {//from   ww  w . ja  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

/**
 * Checks for network connectivity via WiFi or Mobile.
 *
 * @param context Context of application
 * @return A concatenated string with two fields separated by a colon.  The first field is Wifi
 * and second field is Mobile.  If wifi is up, there will be a 'w', otherwise no character.
 * If mobile is up, there will be a 'm', otherwise no character.  If neither is up, then the
 * string will only be the colon./*from w w w . ja va 2 s . c  o m*/
 */
public static String getNetworkStatus(Context context) {

    String result = "";

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

    NetworkInfo activeInfo = connMgr.getActiveNetworkInfo();
    boolean wifiConnected;
    boolean mobileConnected;
    if (activeInfo != null && activeInfo.isConnected()) {
        wifiConnected = activeInfo.getType() == ConnectivityManager.TYPE_WIFI;
        mobileConnected = activeInfo.getType() == ConnectivityManager.TYPE_MOBILE;
    } else {
        wifiConnected = false;
        mobileConnected = false;
    }

    if (wifiConnected)
        result += "w";
    result += ":";
    if (mobileConnected)
        result += "m";
    return result;
}

From source file:com.codeskraps.lolo.misc.Utils.java

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

From source file:com.android.utility.util.Network.java

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

    return (mWifi.isConnected());
}

From source file:Main.java

public static boolean hasConnected(Context context) {
    ConnectivityManager connectMgr = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo mobNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    NetworkInfo wifiNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if ((mobNetInfo == null || !mobNetInfo.isConnected())
            && (wifiNetInfo == null || !wifiNetInfo.isConnected())) {
        return false;
    }/*from   www .  j av a  2s .  co m*/

    return true;
}

From source file:Main.java

public static boolean checkWifiNetwork(Context context) {
    boolean has = false;
    ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = connectivity.getActiveNetworkInfo();
    int netType = info.getType();
    int netSubtype = info.getSubtype();
    if (netType == ConnectivityManager.TYPE_WIFI) {
        has = info.isConnected();
    }//from  ww w.  j av  a2  s  .c o m
    return has;
}

From source file:Main.java

public static boolean isWifi(Context ctx) {
    ConnectivityManager cm = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = cm.getActiveNetworkInfo();
    if (info == null)
        return false;

    int netType = info.getType();
    if (netType == ConnectivityManager.TYPE_WIFI) {
        return info.isConnected();
    } else {/*from  ww  w.j av  a  2s .c  om*/
        return false;
    }
}

From source file:Main.java

/**
 * checks if there is WI-FI connection available
 * @param currentActivity//  w  w w. j a va2s  .c  o m
 * @return
 */
public static boolean isWifiInternetAvailable(Context currentActivity) {
    final ConnectivityManager connectivityManager = (ConnectivityManager) currentActivity
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    if (networkInfo != null) {
        if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
            if (networkInfo.isConnected()) {
                return true;
            }
        }
    }

    return false;
}

From source file:Main.java

/**
 * checks if there is network connection available
 * @param currentActivity//from  w  ww . j a v  a  2  s . c om
 * @return
 */
public static boolean isMobileInternetAvailable(Activity currentActivity) {
    final ConnectivityManager connectivityManager = (ConnectivityManager) currentActivity
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    if (networkInfo != null) {
        if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
            if (networkInfo.isConnected()) {
                return true;
            }
        }
    }

    return false;
}