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:Main.java

/**
 * Requires {@link android.Manifest.permission#ACCESS_WIFI_STATE}
 * and {@link android.Manifest.permission#CHANGE_WIFI_STATE} permissions.
 *
 * @param context//from   ww  w  .  j  a  v  a  2s.  c  o m
 * @return
 */
@SuppressLint("MissingPermission")
public static void reconnectWifi(Context context, int networkId) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    if (ni != null) {
        WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        if (networkId > -1) {
            wm.enableNetwork(networkId, true);
        }
    }
}

From source file:Main.java

/**
 * Requires {@link android.Manifest.permission#ACCESS_WIFI_STATE}
 * and {@link android.Manifest.permission#CHANGE_WIFI_STATE} permissions.
 *
 * @param context/*from  w ww  .java2  s.  c  om*/
 * @return
 */
@SuppressLint("MissingPermission")
public static int disconnectWifi(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    if (ni != null && ni.isConnectedOrConnecting()) {
        WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        int id = wm.getConnectionInfo().getNetworkId();
        return wm.disconnect() ? id : -1;
    }

    return -1;
}

From source file:Main.java

public static boolean isWiFi(Context context) {
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo.State wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
    if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING)
        return true;
    return false;

}

From source file:Main.java

public static boolean isConnectedWifi(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

/**
 * Permet de verifier la connexion tablette est elle en 4 G ou non
 * @param pContext//from w  ww . j a  v  a  2s.  c  o m
 * @return
 */
public static boolean isConnectedWithData(Context pContext) {
    ConnectivityManager connMgr = (ConnectivityManager) pContext.getSystemService(Context.CONNECTIVITY_SERVICE);
    android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    return mobile.isConnectedOrConnecting();
}

From source file:Main.java

public static boolean isWifiOn(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 boolean isOnWifiInternet(Context context) {
    ConnectivityManager connManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    return (info.isAvailable() && info.isConnected());
}

From source file:Main.java

/**
 * method to check for network availability. returns true for available and
 * false for unavailable/*w ww. j a va2 s  .c  om*/
 */
public static boolean isConnectionAvailable(Context context) {
    ConnectivityManager conn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo wifiNetwork = conn.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    NetworkInfo mobileNetwork = conn.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (wifiNetwork != null && wifiNetwork.isAvailable() == true
            && wifiNetwork.isConnectedOrConnecting() == true) {
        return true;
    } else if (mobileNetwork != null && mobileNetwork.isAvailable() == true
            && mobileNetwork.isConnectedOrConnecting() == true) {
        return true;
    } else
        return false;
}

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  a v a 2 s . c  om*/
    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 CheckNetworkConnect(Context context) {
    boolean result = false;
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo mobileInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    NetworkInfo wifiInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    NetworkInfo ethInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
    NetworkInfo activeInfo = manager.getActiveNetworkInfo();
    Log.v("networkInfo", "mobile:" + mobileInfo.isConnected() + "\n" + "wifi:" + wifiInfo.isConnected() + "\n"
            + "eth:" + ethInfo.isConnected() + "\n" + "active:" + activeInfo.getTypeName());
    if (wifiInfo.isConnected() || ethInfo.isConnected()) {
        result = true;/*from  w w w  .j  a v  a  2  s .c  o m*/
    }
    return result;
}