Android Context Check hasConnection(Context c)

Here you can find the source of hasConnection(Context c)

Description

has Connection

Declaration

public static boolean hasConnection(Context c) 

Method Source Code

//package com.java2s;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class Main {
    public static boolean hasConnection(Context c) {

        ConnectivityManager cm = (ConnectivityManager) c
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo wifiNetwork = cm/*from  w ww . j  a  va 2 s  .  c o m*/
                .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (wifiNetwork != null && wifiNetwork.isConnected()) {
            return true;
        }

        NetworkInfo mobileNetwork = cm
                .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (mobileNetwork != null && mobileNetwork.isConnected()) {
            return true;
        }

        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        if (activeNetwork != null && activeNetwork.isConnected()) {
            return true;
        }

        return false;

    }
}

Related

  1. hasAsymmericMasterSecret(Context context)
  2. hasInternetConnection(Context context)
  3. hasInternetConnectivity(Context applicationContext)
  4. hasNotificationPermission(Context context)
  5. hasOtherSPAppsInstalled(Context context)