Android Network State Check checkInternetConnection(Context context)

Here you can find the source of checkInternetConnection(Context context)

Description

check Internet Connection

Declaration

public static boolean checkInternetConnection(Context context) 

Method Source Code

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

public class Main {
    public static boolean checkInternetConnection(Context context) {
        boolean haveConnectedWifi = false;
        boolean haveConnectedMobile = false;

        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        try {/*w w w .  j av  a2 s  .c o m*/
            NetworkInfo[] netInfo = cm.getAllNetworkInfo();
            for (NetworkInfo ni : netInfo) {
                if (ni.getTypeName().equalsIgnoreCase("WIFI"))
                    if (ni.isConnected())
                        haveConnectedWifi = true;
                if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
                    if (ni.isConnected())
                        haveConnectedMobile = true;
            }
        } catch (Exception e) {
            e.getStackTrace();
        }
        return haveConnectedWifi || haveConnectedMobile;
    }
}

Related

  1. isNetworkAvailable(Activity mainActivity)
  2. isDeviceOnline(Activity activity)
  3. isConnected(final Activity act)
  4. isOnline(Activity activity)
  5. CheckInternet(Activity a)