Example usage for android.net NetworkInfo getState

List of usage examples for android.net NetworkInfo getState

Introduction

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

Prototype

@Deprecated
public State getState() 

Source Link

Document

Reports the current coarse-grained state of the network.

Usage

From source file:Main.java

public static boolean isNetworkAvailable(Context context) {
    ConnectivityManager mgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo[] info = mgr.getAllNetworkInfo();
    if (info != null) {
        for (NetworkInfo anInfo : info) {
            if (anInfo.getState() == NetworkInfo.State.CONNECTED) {
                return true;
            }// w ww .j  a v a2s  . c  o m
        }
    }
    return false;
}

From source file:Main.java

public static boolean isOnline(Context context) {
    ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo[] networkInfos = conMgr.getAllNetworkInfo();
    for (NetworkInfo networkInfo : networkInfos) {
        if (networkInfo.getState() == NetworkInfo.State.CONNECTED
                || networkInfo.getState() == NetworkInfo.State.CONNECTING) {
            return true;
        }/*from ww  w.j av a2  s  .  c o  m*/
    }
    return false;
}

From source file:Main.java

public static boolean isOnline(final Context context) {
    final ConnectivityManager conMgr = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    final NetworkInfo[] networkInfos = conMgr.getAllNetworkInfo();
    for (final NetworkInfo networkInfo : networkInfos) {
        if ((networkInfo.getState() == NetworkInfo.State.CONNECTED)
                || (networkInfo.getState() == NetworkInfo.State.CONNECTING)) {
            return true;
        }//  w w  w . j  av a  2s . c om
    }
    return false;
}

From source file:Main.java

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

From source file:Main.java

public static boolean isNetworkAvailable(Context context) {
    Object connectivityService = context.getSystemService(Context.CONNECTIVITY_SERVICE);
    ConnectivityManager connectivityManager = (ConnectivityManager) connectivityService;
    if (connectivityManager == null) {
        return false;
    } else {// w  ww.j a  v  a  2 s  . c om
        NetworkInfo[] networkInfos = connectivityManager.getAllNetworkInfo();
        if (networkInfos != null && networkInfos.length > 0) {
            for (NetworkInfo networkInfo : networkInfos) {
                if (networkInfo.getState() == NetworkInfo.State.CONNECTED) {
                    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.j ava2s . c o  m
        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;
}

From source file:Main.java

public static State getNetworkState(Context act) {
    ConnectivityManager cm = (ConnectivityManager) act.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm == null)
        return null;
    NetworkInfo info = cm.getActiveNetworkInfo();
    if (info != null)
        return info.getState();
    return null;//w ww.  j a  v a 2  s  .  c o  m
}

From source file:Main.java

public static boolean isNetworkAvailable(Context context) {
    boolean status = false;
    try {//from w ww  .  j  a va2 s.com
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getNetworkInfo(0);
        if (netInfo != null && netInfo.getState() == NetworkInfo.State.CONNECTED) {
            status = true;

        } else {
            netInfo = cm.getNetworkInfo(1);
            if (netInfo != null && netInfo.getState() == NetworkInfo.State.CONNECTED)
                status = true;
        }

    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }

    return status;
}

From source file:Main.java

public static boolean isConnectedToTheInternet(Context context) {
    final ConnectivityManager connManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo activeNetwork = connManager.getActiveNetworkInfo();
    return (activeNetwork != null && activeNetwork.getState() == NetworkInfo.State.CONNECTED);
}

From source file:Main.java

public static boolean isConnToInternet(Context context) {
    ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity != null) {
        NetworkInfo[] infoArr = connectivity.getAllNetworkInfo();
        if (isNotEmpty(infoArr)) {
            for (NetworkInfo networkInfo : infoArr) {
                if (networkInfo.getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }//from  w w w  .  j a v  a2 s.  c o m
            }
        }
    }

    return false;
}