Example usage for android.net NetworkInfo getState

List of usage examples for android.net NetworkInfo getState

Introduction

In this page you can find the example usage for android.net NetworkInfo getState.

Prototype

@Deprecated
public State getState() 

Source Link

Document

Reports the current coarse-grained state of the network.

Usage

From source file:Main.java

public static boolean isWifiConnected(Context context) {
    ConnectivityManager mgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (mgr == null)
        return false;
    NetworkInfo info = mgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (info == null)
        return false;
    NetworkInfo.State state = info.getState();
    return state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING;
}

From source file:Main.java

public static String[] getNetType(Context paramContext) {
    String[] arrayOfString = { "Unknown", "Unknown" };
    PackageManager localPackageManager = paramContext.getPackageManager();
    if (localPackageManager.checkPermission("android.permission.ACCESS_NETWORK_STATE",
            paramContext.getPackageName()) != 0) {
        arrayOfString[0] = "Unknown";
        return arrayOfString;
    }/*from w w w.j av  a2  s.  c  o  m*/
    ConnectivityManager localConnectivityManager = (ConnectivityManager) paramContext
            .getSystemService("connectivity");
    if (localConnectivityManager == null) {
        arrayOfString[0] = "Unknown";
        return arrayOfString;
    }
    NetworkInfo localNetworkInfo1 = localConnectivityManager.getNetworkInfo(1);
    if (localNetworkInfo1 != null && localNetworkInfo1.getState() == NetworkInfo.State.CONNECTED) {
        arrayOfString[0] = "2";
        return arrayOfString;
    }
    NetworkInfo localNetworkInfo2 = localConnectivityManager.getNetworkInfo(0);
    if (localNetworkInfo2 != null && localNetworkInfo2.getState() == NetworkInfo.State.CONNECTED) {
        arrayOfString[0] = "1";
        arrayOfString[1] = localNetworkInfo2.getSubtypeName();
        return arrayOfString;
    }
    return arrayOfString;
}

From source file:Main.java

public static boolean isNetworked(Context c) { // Check if we are on a network
    ConnectivityManager mManager = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo current = mManager.getActiveNetworkInfo();
    if (current == null) {
        return false;
    }//  w ww . j a v a2  s .  c o m
    return (current.getState() == NetworkInfo.State.CONNECTED);
}

From source file:Main.java

public static boolean isMobileConnected(Context context) {
    ConnectivityManager mgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (mgr == null)
        return false;
    NetworkInfo info = mgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (info == null)
        return false;
    NetworkInfo.State state = info.getState();
    return state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING;
}

From source file:Main.java

/**
 * Check mobile network and wireless status
 *//*from w  w w. j a  v a2 s.  c o  m*/
public static boolean isNetworkAvailableExt(Context paramContext) {
    ConnectivityManager localConnectivityManager = (ConnectivityManager) paramContext
            .getSystemService("connectivity");
    if (localConnectivityManager == null) {
        return false;
    }
    while (true) {
        //
        NetworkInfo localNetworkInfo = localConnectivityManager.getActiveNetworkInfo();
        if ((localNetworkInfo == null) || (localNetworkInfo.getState() != NetworkInfo.State.CONNECTED))
            return false;
        if (localNetworkInfo.getType() == 1) {
            return true;
        }
        if (localNetworkInfo.getType() == 0) {
            return true;
        }
        return true;
    }
}

From source file:Main.java

public static boolean isNetworkAvailable(Context context) {
    try {//  ww  w  .j  av  a  2s. c  o m
        ConnectivityManager connectivity = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo info = connectivity.getActiveNetworkInfo();
            if (info != null && info.isConnected()) {
                if (info.getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return false;
}

From source file:Main.java

public static NetworkInfo.State getCurrentNetworkState(Context context) {
    NetworkInfo networkInfo = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE))
            .getActiveNetworkInfo();/*from www.j a v a 2  s .co m*/
    return networkInfo != null ? networkInfo.getState() : null;
}

From source file:Main.java

static public boolean isDisconnectedIntent(Intent intent) {
    boolean res = false;
    NetworkInfo networkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
    if (networkInfo != null) {
        NetworkInfo.State state = networkInfo.getState();
        res = (state.equals(NetworkInfo.State.DISCONNECTING) || state.equals(NetworkInfo.State.DISCONNECTED))
                && (networkInfo.getType() == ConnectivityManager.TYPE_WIFI);

    } else {/*from   w  w w .j av  a2 s.co m*/
        int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN);
        if (wifiState == WifiManager.WIFI_STATE_DISABLED || wifiState == WifiManager.WIFI_STATE_DISABLING) {
            res = true;
        }
    }
    return res;
}

From source file:Main.java

public static boolean checkNetworkConnectivity(Context context) {
    // get the system ConnectivityManager
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    if (networkInfo != null && (networkInfo.isAvailable() || networkInfo.isConnectedOrConnecting())) {
        if (networkInfo.getState() == NetworkInfo.State.CONNECTED) {
            return true;
        }//  ww w .ja  v a2  s.  c  om
        // record the fact that there is live connection
    }
    return false;

}

From source file:Main.java

public static boolean isNetworkAvailable(Context context) {
    if (context != null) {
        ConnectivityManager manager = (ConnectivityManager) context.getApplicationContext()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (manager == null) {
            return false;
        }/*from  www.jav a2s  .com*/
        NetworkInfo networkinfo = manager.getActiveNetworkInfo();
        if (networkinfo == null || !networkinfo.isAvailable()) {
            return false;
        }

        if (networkinfo.getState() == NetworkInfo.State.CONNECTED) {
            return true;
        } else {
            return false;
        }
    }
    return false;
}