Android Network State Check hasConnectivity(final NetworkInfo info)

Here you can find the source of hasConnectivity(final NetworkInfo info)

Description

has Connectivity

License

Open Source License

Declaration

private static boolean hasConnectivity(final NetworkInfo info) 

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   w ww.  j av a2s .c  o 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. checkNetWorkStatus(Context context)
  2. checkNetworkState(Context context)
  3. getMobileState(Context context)
  4. hasConnectivity(Application application)
  5. hasConnectivity(final Context context, final int... networkTypes)
  6. hasInternetAvailable(Context context)
  7. hasMobileConnectivity(Context context)
  8. hasNoConnectivity(Application application)
  9. isConnected(Context context)