Example usage for android.net ConnectivityManager getActiveNetworkInfo

List of usage examples for android.net ConnectivityManager getActiveNetworkInfo

Introduction

In this page you can find the example usage for android.net ConnectivityManager getActiveNetworkInfo.

Prototype

@Deprecated
@RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
@Nullable
public NetworkInfo getActiveNetworkInfo() 

Source Link

Document

Returns details about the currently active default data network.

Usage

From source file:Main.java

public static boolean isConnectionAvailable(final Context context) {
    final ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

    return networkInfo != null && networkInfo.isConnected();
}

From source file:hoahong.facebook.messenger.fbclient.MMConnector.java

public static boolean isOnline(Context activity) {
    ConnectivityManager cm = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    }/*from  w ww  . j av a  2  s.  c  o  m*/
    return false;
}

From source file:Main.java

public static boolean isNetworkConnected(Context context) {
    ConnectivityManager mgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (mgr == null)
        return false;
    NetworkInfo info = mgr.getActiveNetworkInfo();
    return info != null && info.isConnectedOrConnecting();
}

From source file:Main.java

public static boolean hasInternet(Activity activity) {

    ConnectivityManager manager = (ConnectivityManager) activity

            .getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo info = manager.getActiveNetworkInfo();

    if (info == null || !info.isConnected()) {

        return false;
    }//from   w  ww. ja v  a 2  s.c om
    if (info.isRoaming()) {
        // here is the roaming option you can change it if you want to

        // disable internet while roaming, just return false

        return true;
    }
    return true;
}

From source file:Main.java

public static boolean isWifiConnectionAvailable(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    if (activeNetworkInfo != null) {
        return activeNetworkInfo.isConnected() && activeNetworkInfo.getType() == connectivityManager.TYPE_WIFI;
    } else {//from   ww w.  jav a2s  .  c  o  m
        return false;
    }
}

From source file:Main.java

public static boolean isInternetAvailable(Context context) {
    if (context != null) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        return connectivityManager.getActiveNetworkInfo() != null
                && connectivityManager.getActiveNetworkInfo().isConnectedOrConnecting();
    } else {/*from   w w  w. j  a v a  2  s  . com*/
        return NO;
    }
}

From source file:Main.java

public static boolean isNetworkAvailable(Context context) {
    ConnectivityManager mgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (mgr == null)
        return false;
    NetworkInfo info = mgr.getActiveNetworkInfo();
    return info != null && info.isAvailable();
}

From source file:Main.java

/**
 * Check for 3g internet connectivity//from   w  ww . j a v  a  2  s .c  o  m
 *
 * @param context context
 * @return hasInternet boolean
 */
public static boolean has3gInternetAccess(@NonNull Context context) {
    try {
        boolean has3gInternet = false;
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        NetworkInfo mobile = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (mobile != null && mobile.isAvailable() && // !ic_mobile.isRoaming() &&
                networkInfo != null && networkInfo.isConnectedOrConnecting()) {

            has3gInternet = true;
        }

        return has3gInternet;
    } catch (Exception e) {
        return false;
    }
}

From source file:Main.java

public static boolean isNetworkAvailable(Context context) {
    try {//from   ww w.ja  v a 2  s  .  c  o  m
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm != null) {
            NetworkInfo ni = cm.getActiveNetworkInfo();
            if (ni != null && ni.isConnected()) {
                if (ni.getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
    } catch (Exception e) {
        return false;
    }
    return false;
}

From source file:Main.java

public static boolean isNetworkConnected(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    return networkInfo != null && networkInfo.isConnected();
}