Example usage for android.net ConnectivityManager getActiveNetworkInfo

List of usage examples for android.net ConnectivityManager getActiveNetworkInfo

Introduction

In this page you can find the example usage for android.net ConnectivityManager getActiveNetworkInfo.

Prototype

@Deprecated
@RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
@Nullable
public NetworkInfo getActiveNetworkInfo() 

Source Link

Document

Returns details about the currently active default data network.

Usage

From source file:Main.java

public static boolean isConnected(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    if (connectivityManager.getActiveNetworkInfo() != null
            && connectivityManager.getActiveNetworkInfo().isAvailable()
            && connectivityManager.getActiveNetworkInfo().isConnected()) {
        return true;

    } else {/*from  w  ww .  j  a v  a 2s. co  m*/
        return false;
    }
}

From source file:Main.java

public static boolean checkConnection(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    if (networkInfo != null) {
        return networkInfo.isAvailable();
    }//from w  w  w . j ava  2 s  .  co m
    return false;
}

From source file:Main.java

public static boolean isNetConnected(Context context) {
    if (context == null) {
        return true;
    }//from w w w.  jav  a 2 s .  c o m
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo nwInfo = connectivityManager.getActiveNetworkInfo();
    if (nwInfo != null && nwInfo.isConnectedOrConnecting()) {
        return true;
    }
    return false;
}

From source file:Main.java

public static boolean isConnectedToTheInternet(Context context) {
    final ConnectivityManager connManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo activeNetwork = connManager.getActiveNetworkInfo();
    return (activeNetwork != null && activeNetwork.getState() == NetworkInfo.State.CONNECTED);
}

From source file:Main.java

/**
 * Returns true if the network is connected.
 * @param context Context associated with the activity.
 *//* w  ww  .j  a  va  2 s . c om*/
public static boolean isConnected(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    return networkInfo != null && networkInfo.isConnected();
}

From source file:Main.java

public static boolean isNetworkConnected(Context context) {
    boolean resp = false;
    final ConnectivityManager connMgr = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetInfo = connMgr.getActiveNetworkInfo();
    if (activeNetInfo != null && activeNetInfo.isAvailable()) {
        resp = true;/*from ww  w. j av  a2  s .  c o  m*/
    }
    return resp;
}

From source file:Main.java

public static boolean checkNetworkStatus(Context context) {
    boolean resp = false;
    final ConnectivityManager connMgr = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetInfo = connMgr.getActiveNetworkInfo();
    if (activeNetInfo != null && activeNetInfo.isAvailable()) {
        resp = true;/*from w ww  .  ja v  a 2  s  .co m*/
    }
    return resp;
}

From source file:Main.java

public static boolean isInternetConnected(Context context) {

    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

    return networkInfo != null && networkInfo.isConnectedOrConnecting();
}

From source file:Main.java

public static boolean isNetConnect(@NonNull Context context) {
    ConnectivityManager service = (ConnectivityManager) context.getApplicationContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = service.getActiveNetworkInfo();

    return info != null && info.isAvailable();
}

From source file:Main.java

public static boolean isURLConnectable(Context context, String url) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnected()) {
        try {/*from   w w w .  j ava 2 s .  c o  m*/
            HttpURLConnection urlc = (HttpURLConnection) new URL(url).openConnection();
            urlc.setConnectTimeout(10 * 1000); // 10 s.
            urlc.connect();
            if (urlc.getResponseCode() == 200) { // 200 = "OK" code (http
                // connection is fine).
                Log.wtf("Connection", "Success !");
                return true;
            } else {
                return false;
            }
        } catch (MalformedURLException e1) {
            return false;
        } catch (IOException e) {
            return false;
        }
    }
    return false;
}