Example usage for android.net NetworkInfo isConnected

List of usage examples for android.net NetworkInfo isConnected

Introduction

In this page you can find the example usage for android.net NetworkInfo isConnected.

Prototype

@Deprecated
public boolean isConnected() 

Source Link

Document

Indicates whether network connectivity exists and it is possible to establish connections and pass data.

Usage

From source file:Main.java

public static boolean isNetworkAvailable() {
    Context c = getContext();//from  w  w  w.  ja  v a 2  s . c o m
    ConnectivityManager connectivityManager = (ConnectivityManager) c
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    boolean errorInNetwork = activeNetworkInfo != null && activeNetworkInfo.isConnected();
    return errorInNetwork;
}

From source file:Main.java

public static boolean Is3GAvailable(Context context) {
    ConnectivityManager m_NetConnectMgr = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    boolean bConnect = false;
    try {//  ww  w .  j a v a  2 s. c  om
        if (m_NetConnectMgr == null)
            return false;
        NetworkInfo info = m_NetConnectMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        bConnect = (info.isAvailable() && info.isConnected());
    } catch (Exception e) {
        return false;
    }

    return bConnect;
}

From source file:Main.java

/**
 * @param context//from w  w  w  .jav a 2s  .c o  m
 * @return true if the device has an internet connection
 */
public final static boolean hasInternetConnection(Context context) {
    if (PackageManager.PERMISSION_GRANTED == context
            .checkCallingOrSelfPermission(Manifest.permission.ACCESS_NETWORK_STATE)) {
        final ConnectivityManager connManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        final NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
        if (networkInfo != null) {
            return networkInfo.isConnected();
        }
    }
    return false;
}

From source file:Main.java

/**
 * check if network is available//from   ww  w  .j  a  v  a  2 s . c  o  m
 * @return
 * @param context
 */
public static boolean networkAvailable(Context context) {
    // get network manager
    ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    // get network info
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    // check network status
    if (networkInfo != null && networkInfo.isConnected()) {
        // fetch data
        return true;
    } else {
        // display error
        return false;
    }
}

From source file:Main.java

/**
 * method to check if internet is connected or not
 *
 * @param mContext Context/*  w w w. j a v  a 2  s  .  c  o  m*/
 * @return true if connected else false
 */
public static boolean isInternetAvailable(Context mContext) {

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

    NetworkInfo wifiNetwork = cm.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;
}

From source file:Main.java

public static boolean isWifiConnected() {
    NetworkInfo ni = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    return ni.isConnected();
}

From source file:Main.java

public static boolean IsWifiAvailable(Context context) {
    ConnectivityManager m_NetConnectMgr = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    boolean bConnect = false;
    try {/* w ww  .  j a v a2  s  .co m*/
        if (m_NetConnectMgr == null)
            return false;

        NetworkInfo info = m_NetConnectMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        bConnect = (info.isAvailable() && info.isConnected());

    } catch (Exception e) {
        return false;
    }

    return bConnect;
}

From source file:Main.java

static public synchronized boolean checkConnectivity(Context context) {

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    // mMobile = prefs.getBoolean(ENUMPrefs.ENUM_PREF_MOBILE, false);
    mOnline = false;// w w w. j av a  2  s . c  om

    ConnectivityManager mCmgr = (ConnectivityManager) context.getSystemService(Service.CONNECTIVITY_SERVICE);

    NetworkInfo ni = mCmgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (ni != null && ni.isConnected()) { /* wifi is on */
        mOnline = true;
    } else {
        if (mMobile) {
            /* if mobile is active and EDGE or better, we're good */
            ni = mCmgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            if (ni != null && ni.isConnected()) {
                mOnline = (ni.getSubtype() >= TelephonyManager.NETWORK_TYPE_EDGE);
            }
        }
    }
    return mOnline;
}

From source file:Main.java

public static int getNetState() {
    if (cm == null)
        return STATE_UNKNOWN;

    try {/* ww w.j  ava2  s  . com*/
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        if (activeNetwork == null || !activeNetwork.isConnected())
            return STATE_DISCONNECTED;

        if (activeNetwork.isRoaming())
            return STATE_ROAMING;

        return STATE_CONNECTED;
    } catch (Exception e) {
        /* java.lang.SecurityException can occur if
           android.permission.ACCESS_NETWORK_STATE is not given */
        Log.d(TAG, "ConnectivityManager failed", e);
        return STATE_UNKNOWN;
    }
}

From source file:Main.java

public static boolean checkInternet(Activity act) {
    ConnectivityManager conMgr = (ConnectivityManager) act.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo i = conMgr.getActiveNetworkInfo();
    if (i == null) {
        return false;
    }//w  w  w  .j  a  v  a 2s.c  om
    if (!i.isConnected()) {
        return false;
    }
    if (!i.isAvailable()) {
        return false;
    }
    return true;
}