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 int isNetworkAvailable(Activity act) {
    ConnectivityManager connectivityManager = (ConnectivityManager) act
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    if (activeNetworkInfo != null && activeNetworkInfo.isConnected())
        return 1;
    else//from   w  ww .j a va 2  s .c om
        return 0;
}

From source file:Main.java

public static boolean is3G(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
    if (activeNetInfo != null && activeNetInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
        return true;
    }//w ww  . j  ava  2s. com
    return false;
}

From source file:Main.java

public static boolean isNetworkConnected(Context context) {
    if (context == null) {
        return false;
    }/*from  ww w  .j a  v a2s.c o  m*/
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
    return (activeNetInfo != null) && (activeNetInfo.isConnected());
}

From source file:Main.java

/**
 * Checks if a network is currently enabled on the device
 * //from   w w  w . j  a  v a  2 s. c om
 * @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

public static boolean isWifiConnected(Context context) {
    if (context == null) {
        return false;
    }//from   w  ww.  j  a v  a2 s .c o  m
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
    return (activeNetInfo != null) && (activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI);
}

From source file:Main.java

public static boolean hasInternetConnection(Context context) {
    final ConnectivityManager conMgr = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();
    if (activeNetwork != null && activeNetwork.isConnected()) {
        return true;
    }//from   www .  jav  a2s .  c o m

    return false;
}

From source file:Main.java

public static boolean isConnected(Context context) {
    if (context != null) {
        ConnectivityManager conn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (conn != null) {
            NetworkInfo info = conn.getActiveNetworkInfo();
            return (info != null && info.isConnected());
        }//from ww w .  ja  va 2  s.c  o  m
    }
    return false;
}

From source file:Main.java

public static boolean isWifiConnected(Context mContext) {
    ConnectivityManager connectivityManager = (ConnectivityManager) mContext
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
    if (activeNetInfo != null && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {
        return true;
    }/*  w  w  w  .j av a2 s .co m*/
    return false;
}

From source file:Main.java

public static State getNetworkState(Context act) {
    ConnectivityManager cm = (ConnectivityManager) act.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm == null)
        return null;
    NetworkInfo info = cm.getActiveNetworkInfo();
    if (info != null)
        return info.getState();
    return null;/*w w w  .  j  a va  2s. c  om*/
}

From source file:Main.java

public static String isNetWorkType(Context cxt) {
    ConnectivityManager connectivityManager = (ConnectivityManager) cxt
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
    if (activeNetInfo != null && activeNetInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
        return "3G";
    }//from w w w  .j  a  v a 2 s  .  c o  m
    if (activeNetInfo != null && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {
        return "WIFI";
    }
    return "";
}