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 context) {
    if (context == null)
        return false;

    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

From source file:Main.java

public static boolean isOnline(Context context) {
    boolean haveConnectedWifi = false;
    boolean haveConnectedMobile = false;
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    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;
    }// w w w  . j a v  a 2  s.  c  o m
    return haveConnectedWifi || haveConnectedMobile;
}

From source file:Main.java

public static boolean acquireWifiLock(Context context) {
    //We only allow backup on wifi connection. Make sure we are connected and if so lock the wifi connection
    ConnectivityManager connManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo wifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (wifi.isConnected()) {
        if (wifiLock == null) {
            WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            wifiLock = wm.createWifiLock(WifiManager.WIFI_MODE_FULL, "com.zns.comicdroid.wifilock");
            wifiLock.setReferenceCounted(true);
            wifiLock.acquire();// w  ww.j  a v a2  s  .c o m
        } else {
            wifiLock.acquire();
        }
        return true;
    }
    return false;
}

From source file:Main.java

/**
 * Checks if a network is currently enabled on the device
 * /*  w w  w  .ja  v a  2  s  .  c  o m*/
 * @param ctx
 * @return
 */
public static boolean isNetworkAvailable(Context ctx) {
    ConnectivityManager connectivityManager = (ConnectivityManager) ctx
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

From source file:Main.java

/**
 * Check to see if the network is available.
 * @return true if there is access to the internet, false otherwise.
 *///from www .j  a v  a  2 s  .c  o m
public static boolean isNetworkAvailable(Context context) {
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = manager.getActiveNetworkInfo();
    boolean isAvailable = false;
    if (info != null && info.isConnected()) {
        isAvailable = true;
    }
    Log.v("TheNetUtil", "isNetworkAvailable(). net is " + isAvailable);
    return isAvailable;
}

From source file:Main.java

/**
 * Function for check the network connectivity
 * //w ww. j a v a  2  s .  c  om
 * @return true if network Available otherwise false
 */
public static boolean isNetworkAvailable(Context context) {
    if (context
            .checkCallingOrSelfPermission(Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED) {
        return false;
    }

    ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo netInfo = connMgr.getActiveNetworkInfo();
    return netInfo != null && netInfo.isConnected();
}

From source file:Main.java

/**
 * Returns true if network is available//from w w w  .  j av a 2 s  .  co m
 */
static boolean isNetworkAvailable(Context context) {
    // Copied from
    // https://udacity-github-sync-content.s3.amazonaws.com/_imgs/752/1436612063/Screen_Shot_2015-07-11_at_12.53.52.png
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

From source file:Main.java

public static boolean checkConnection(Activity a) {
    boolean hasConnectedWifi = false;
    boolean hasConnectedMobile = false;

    ConnectivityManager cm = (ConnectivityManager) a.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo[] netInfo = cm.getAllNetworkInfo();
    for (NetworkInfo ni : netInfo) {
        if (ni.getTypeName().equalsIgnoreCase("wifi"))
            if (ni.isConnected())
                hasConnectedWifi = true;
        if (ni.getTypeName().equalsIgnoreCase("mobile"))
            if (ni.isConnected())
                hasConnectedMobile = true;
    }//ww w  . jav  a2  s. co  m
    return hasConnectedWifi || hasConnectedMobile;
}

From source file:Main.java

public static boolean hasConnection(final Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo wifiInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (wifiInfo != null && wifiInfo.isConnected()) {
        return true;
    }//from ww w. j ava 2s .  com
    wifiInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (wifiInfo != null && wifiInfo.isConnected()) {
        return true;
    }
    wifiInfo = cm.getActiveNetworkInfo();
    if (wifiInfo != null && wifiInfo.isConnected()) {
        return true;
    }
    return false;
}

From source file:Main.java

public static boolean isWIFIConnected(Context context) {
    ConnectivityManager cmg = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    boolean result = true;
    NetworkInfo active = cmg.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (active == null || !active.isConnected()) {
        result = false;//from   w  w w  . ja v a  2 s.  c o m
    }
    return result;
}