Android Network State Check isNetworkAvailable()

Here you can find the source of isNetworkAvailable()

Description

Uses Android connectivity service to give information about the availability of the network on the phone.

Return

true if the network is available.

Declaration

public static boolean isNetworkAvailable() 

Method Source Code

//package com.java2s;
import android.content.Context;

import android.net.ConnectivityManager;

public class Main {
    /**//from   w  ww  .  j ava  2s . c o m
     * The context of the Android application or widget.
     */
    private static Context context;

    /**
     * Uses Android connectivity service to give information about the 
     * availability of the network on the phone.
     * 
     * @return
     *       true if the network is available.
     */
    public static boolean isNetworkAvailable() {
        if (context != null) {
            ConnectivityManager connMgr = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);

            return connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
                    .isConnected()
                    || connMgr.getNetworkInfo(
                            ConnectivityManager.TYPE_MOBILE).isConnected();
        }

        return false;
    }
}

Related

  1. isConnectedMobile(Context context)
  2. isConnectedMobile(Context context)
  3. isConnectingToInternet()
  4. isConnectingToInternet(Context context)
  5. isConnectionFast(int type, int subType)
  6. isNetworkConnected(Context context)
  7. isOnline(Context c)
  8. isOnline(Context context)
  9. isNetworkReady(Context context)