Android Network State Check isConnectNetWork(final Context context)

Here you can find the source of isConnectNetWork(final Context context)

Description

Detect whether network is available or not

Return

if WIFI or GRPS is available, return YES, else return NO

Declaration

public static boolean isConnectNetWork(final Context context) 

Method Source Code

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

import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import android.util.Log;

public class Main {
    public static final String TAG = "CLDeviceUtil";

    /**//from   ww w  . ja  v  a2 s.  c om
     * Detect whether network is available or not
     * 
     * @return if WIFI or GRPS is available, return YES, else return NO
     * @author Sean Zheng
     * @CreateDate 2013-4-24
     */
    public static boolean isConnectNetWork(final Context context) {
        boolean result = false;
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        boolean isWifiConnected = cm.getNetworkInfo(
                ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED ? true
                : false;
        NetworkInfo mobileState = cm
                .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        boolean isGprsConnected = false;
        if (mobileState != null) {
            isGprsConnected = mobileState.getState() == NetworkInfo.State.CONNECTED ? true
                    : false;
        }
        Log.d(TAG, "wifi connected:" + isWifiConnected + " gps connected:"
                + isGprsConnected);
        result = isWifiConnected || isGprsConnected ? true : false;
        return result;
    }
}

Related

  1. isNetworkConnected(Context context)
  2. isOnline(Context c)
  3. isOnline(Context context)
  4. isNetworkReady(Context context)
  5. isConMeNetwork(String ssid)
  6. isOnline(Context context)
  7. isNetworkAvailable(Context context)
  8. isNetworkAvailable(Context context)
  9. isMobileOnline(Context context)