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 IsNetworkAvailable(Context context) {

    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
    if (connectivityManager != null && netInfo != null) {

        if (netInfo.isConnectedOrConnecting()) {
            return true;
        } else {//w  ww .  jav  a  2  s  . com
            return false;
        }
    } else {
        return false;
    }
}

From source file:Main.java

/**
 * Checks for network connectivity via WiFi or Mobile.
 *
 * @param context Context of application
 * @return A concatenated string with two fields separated by a colon.  The first field is Wifi
 * and second field is Mobile.  If wifi is up, there will be a 'w', otherwise no character.
 * If mobile is up, there will be a 'm', otherwise no character.  If neither is up, then the
 * string will only be the colon./*from w  ww .  j ava2 s .c om*/
 */
public static String getNetworkStatus(Context context) {

    String result = "";

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

    NetworkInfo activeInfo = connMgr.getActiveNetworkInfo();
    boolean wifiConnected;
    boolean mobileConnected;
    if (activeInfo != null && activeInfo.isConnected()) {
        wifiConnected = activeInfo.getType() == ConnectivityManager.TYPE_WIFI;
        mobileConnected = activeInfo.getType() == ConnectivityManager.TYPE_MOBILE;
    } else {
        wifiConnected = false;
        mobileConnected = false;
    }

    if (wifiConnected)
        result += "w";
    result += ":";
    if (mobileConnected)
        result += "m";
    return result;
}

From source file:Main.java

public static NetworkInfo getActiveNetworkInfo(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    if (activeNetworkInfo != null) {
        Log.i(TAG, "N/W Type: " + activeNetworkInfo.getTypeName());
    }/*from  w  ww.j  av  a2s  . c  o m*/
    return activeNetworkInfo;
}

From source file:Main.java

/**
 *
 * Check whether network is connected or not
 *
 * @param context Context of application
 * @return true if connected false otherwise
 *///  w  w  w.  ja  va2  s  . co  m
public static boolean isNetworkAvailable(Context context) {

    boolean connected = false;
    ConnectivityManager connectivityManager = null;
    connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
    if (netInfo != null) {
        connected = netInfo.isConnected();
    }
    return connected;
}

From source file:Main.java

public static boolean isNetWorkConnected(Context context) {
    if (context != null) {
        ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
        if (mNetworkInfo != null) {
            return mNetworkInfo.isAvailable();
        }//from ww w .  j  a v  a  2 s .c  o  m
    }

    return false;
}

From source file:Main.java

public static boolean isNetWorkConnected(Context context) {
    if (context != null) {
        ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
        if (mNetworkInfo != null) {
            return mNetworkInfo.isAvailable() && mNetworkInfo.isConnected();
        }//from w w w  .  j a v a 2s. c om
    }

    return false;
}

From source file:Main.java

public static int hasActiveNetworkInfo(Context context) {
    int state = 0; // Assume disabled
    final ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo mActiveNetworkInfo = cm.getActiveNetworkInfo();
    if (mActiveNetworkInfo != null) {
        String typeName = mActiveNetworkInfo.getTypeName().toLowerCase();
        boolean isConnected = mActiveNetworkInfo.isConnectedOrConnecting();
        int type = mActiveNetworkInfo.getType();
        if ((isNetworkTypeMobile(type)) && (typeName.contains("mobile")) && isConnected)
            state = 2;/*from  www.  j  a  va  2s . c o m*/
        else if ((!isNetworkTypeMobile(type)) && (!typeName.contains("mobile")) && isConnected)
            state = 1;
    }
    return state;
}

From source file:Main.java

public static int getConnectedType(Context context) {
    if (context != null) {
        ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
        if (mNetworkInfo != null && mNetworkInfo.isAvailable()) {
            return mNetworkInfo.getType();
        }/*from  w w w  .  jav  a  2 s  .com*/
    }
    return -1;
}

From source file:Main.java

public static boolean isNetworkAvail(Context context) {
    if (context != null) {
        ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
        if (mNetworkInfo != null) {
            return mNetworkInfo.isAvailable();
        }//from  ww w .j  a v  a  2s .c  om
    }
    return false;
}

From source file:Main.java

public static int getNetWorkType(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager == null ? null : connectivityManager.getActiveNetworkInfo();
    return networkInfo == null ? -1 : networkInfo.getType();
}