Example usage for android.net ConnectivityManager getNetworkInfo

List of usage examples for android.net ConnectivityManager getNetworkInfo

Introduction

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

Prototype

@Deprecated
@RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
@Nullable
public NetworkInfo getNetworkInfo(@Nullable Network network) 

Source Link

Document

Returns connection status information about a particular Network.

Usage

From source file:com.emacs.xpets.utils.Utils.java

public static boolean isNetworkAvailable(Context context) {
    boolean status = false;
    try {/*from w w  w.  ja v  a 2  s  .c  o  m*/
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getNetworkInfo(0);
        if (netInfo != null && netInfo.getState() == NetworkInfo.State.CONNECTED) {
            status = true;
        } else {
            netInfo = cm.getNetworkInfo(1);
            if (netInfo != null && netInfo.getState() == NetworkInfo.State.CONNECTED)
                status = true;
        }
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return status;
}

From source file:com.android.utility.util.Network.java

public static boolean isWifiConnected(Context context) {
    ConnectivityManager connManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    return (mWifi.isConnected());
}

From source file:Main.java

public static String[] getNetworkState(Context pContext) {
    String[] type = new String[2];
    type[0] = "Unknown";
    type[1] = "Unknown";
    if (pContext.getPackageManager().checkPermission("android.permission.ACCESS_NETWORK_STATE",
            pContext.getPackageName()) == PackageManager.PERMISSION_GRANTED) {
        ConnectivityManager localConnectivityManager = (ConnectivityManager) pContext
                .getSystemService("connectivity");
        if (localConnectivityManager == null)
            return type;

        NetworkInfo localNetworkInfo1 = localConnectivityManager.getNetworkInfo(1);
        if ((localNetworkInfo1 != null) && (localNetworkInfo1.getState() == NetworkInfo.State.CONNECTED)) {
            type[0] = "Wi-Fi";
            type[1] = localNetworkInfo1.getSubtypeName();
            return type;
        }/*  w ww. jav a 2  s .c o m*/
        NetworkInfo localNetworkInfo2 = localConnectivityManager.getNetworkInfo(0);
        if ((localNetworkInfo2 == null) || (localNetworkInfo2.getState() != NetworkInfo.State.CONNECTED))
            type[0] = "2G/3G";
        type[1] = localNetworkInfo2.getSubtypeName();
        return type;
    }
    return type;
}

From source file:Main.java

/**
 * Used to determine if there is an active data connection and what type of
 * connection it is if there is one//from   w  w  w.  ja  v a 2 s .c o m
 * 
 * @param context The {@link Context} to use
 * @return True if there is an active data connection, false otherwise.
 *         Also, if the user has checked to only download via Wi-Fi in the
 *         settings, the mobile data and other network connections aren't
 *         returned at all
 */
public static final boolean isOnline(final Context context) {
    /*
     * This sort of handles a sudden configuration change, but I think it
     * should be dealt with in a more professional way.
     */
    if (context == null) {
        return false;
    }

    boolean state = false;
    final boolean onlyOnWifi = false;// PreferenceUtils.getInstace(context).onlyOnWifi();

    /* Monitor network connections */
    final ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    /* Wi-Fi connection */
    final NetworkInfo wifiNetwork = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (wifiNetwork != null) {
        state = wifiNetwork.isConnectedOrConnecting();
    }

    /* Mobile data connection */
    final NetworkInfo mbobileNetwork = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (mbobileNetwork != null) {
        if (!onlyOnWifi) {
            state = mbobileNetwork.isConnectedOrConnecting();
        }
    }

    /* Other networks */
    final NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
    if (activeNetwork != null) {
        if (!onlyOnWifi) {
            state = activeNetwork.isConnectedOrConnecting();
        }
    }

    return state;
}

From source file:Main.java

public static boolean Is3GAvailable(Context context) {
    ConnectivityManager m_NetConnectMgr = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    boolean bConnect = false;
    try {//from   w ww .j a v  a  2s.c o  m
        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

/**
 * Used to determine if there is an active data connection and what type of
 * connection it is if there is one//from   w  ww .j  ava 2 s . c  o m
 * 
 * @param context The {@link Context} to use
 * @return True if there is an active data connection, false otherwise.
 *         Also, if the user has checked to only download via Wi-Fi in the
 *         settings, the mobile data and other network connections aren't
 *         returned at all
 */
public static final boolean isOnline(final Context context) {
    /*
     * This sort of handles a sudden configuration change, but I think it
     * should be dealt with in a more professional way.
     */
    if (context == null) {
        return false;
    }

    boolean state = false;

    /* Monitor network connections */
    final ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    /* Wi-Fi connection */
    final NetworkInfo wifiNetwork = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (wifiNetwork != null) {
        state = wifiNetwork.isConnectedOrConnecting();
    }

    /* Mobile data connection */
    final NetworkInfo mbobileNetwork = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (mbobileNetwork != null) {
        state = mbobileNetwork.isConnectedOrConnecting();
    }

    /* Other networks */
    final NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
    if (activeNetwork != null) {
        state = activeNetwork.isConnectedOrConnecting();
    }

    return state;
}

From source file:Main.java

/**
 * Used to determine if there is an active data connection and what type of
 * connection it is if there is one//from w w w .jav a2 s  .com
 * 
 * @param context The {@link Context} to use
 * @return True if there is an active data connection, false otherwise.
 *         Also, if the user has checked to only download via Wi-Fi in the
 *         settings, the mobile data and other network connections aren't
 *         returned at all
 */
public static final boolean isOnline(final Context context) {
    /*
     * This sort of handles a sudden configuration change, but I think it
     * should be dealt with in a more professional way.
     */
    if (context == null) {
        return false;
    }

    boolean state = false;
    final boolean onlyOnWifi = true;//PreferenceUtils.getInstace(context).onlyOnWifi();

    /* Monitor network connections */
    final ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    /* Wi-Fi connection */
    final NetworkInfo wifiNetwork = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (wifiNetwork != null) {
        state = wifiNetwork.isConnectedOrConnecting();
    }

    /* Mobile data connection */
    final NetworkInfo mbobileNetwork = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (mbobileNetwork != null) {
        if (!onlyOnWifi) {
            state = mbobileNetwork.isConnectedOrConnecting();
        }
    }

    /* Other networks */
    final NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
    if (activeNetwork != null) {
        if (!onlyOnWifi) {
            state = activeNetwork.isConnectedOrConnecting();
        }
    }

    return state;
}

From source file:Main.java

public static boolean IsWifiAvailable(Context context) {
    ConnectivityManager m_NetConnectMgr = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    boolean bConnect = false;
    try {/*from www  .  j  a va 2  s  .c  o  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:in.huohua.peterson.network.NetworkUtils.java

public static boolean isWifiConnected(Context context) {
    ConnectivityManager connManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    return mWifi.isConnected();
}

From source file:com.tingtingapps.securesms.mms.OutgoingLegacyMmsConnection.java

public static boolean isConnectionPossible(Context context) {
    try {/*from   w  w w  .  j  a va2s  .  c om*/
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getNetworkInfo(MmsRadio.TYPE_MOBILE_MMS);
        if (networkInfo == null) {
            Log.w(TAG, "MMS network info was null, unsupported by this device");
            return false;
        }

        getApn(context);
        return true;
    } catch (ApnUnavailableException e) {
        Log.w(TAG, e);
        return false;
    }
}