Android Network State Check hasConnectivity(final Context context, final int... networkTypes)

Here you can find the source of hasConnectivity(final Context context, final int... networkTypes)

Description

has Connectivity

License

Open Source License

Declaration

public static final boolean hasConnectivity(final Context context,
            final int... networkTypes) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class Main {
    public static final boolean hasConnectivity(final Context context,
            final int... networkTypes) {
        final ConnectivityManager connectivity = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (networkTypes.length > 0)
            for (final int networkType : networkTypes)
                if (ConnectivityManager.isNetworkTypeValid(networkType))
                    return hasConnectivity(connectivity
                            .getNetworkInfo(networkType));
        return hasConnectivity(connectivity.getActiveNetworkInfo());
    }/*from   ww  w.  ja v a2 s .  co  m*/

    private static boolean hasConnectivity(final NetworkInfo info) {
        return null != info && info.isAvailable() && info.isConnected();
    }

    public static final boolean isConnected(final Context context,
            final int... networkTypes) {
        final ConnectivityManager connectivity = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (networkTypes.length > 0)
            for (final int networkType : networkTypes)
                if (ConnectivityManager.isNetworkTypeValid(networkType))
                    return isConnected(connectivity
                            .getNetworkInfo(networkType));
        return isConnected(connectivity.getActiveNetworkInfo());
    }

    private static boolean isConnected(final NetworkInfo info) {
        return null != info && info.isConnected();
    }
}

Related

  1. checkNetState(Context context)
  2. checkNetWorkStatus(Context context)
  3. checkNetworkState(Context context)
  4. getMobileState(Context context)
  5. hasConnectivity(Application application)
  6. hasConnectivity(final NetworkInfo info)
  7. hasInternetAvailable(Context context)
  8. hasMobileConnectivity(Context context)
  9. hasNoConnectivity(Application application)