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

/**
 * checks if there is network connection available
 * @param currentActivity/*w w w . j a v  a 2  s .  c  o  m*/
 * @return
 */
public static boolean isMobileInternetAvailable(Activity currentActivity) {
    final ConnectivityManager connectivityManager = (ConnectivityManager) currentActivity
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    if (networkInfo != null) {
        if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
            if (networkInfo.isConnected()) {
                return true;
            }
        }
    }

    return false;
}

From source file:Main.java

public static boolean isWifi(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    if (null == cm) {
        return false;
    }/*from  w  ww. j a  v  a 2 s .  com*/

    NetworkInfo info = cm.getActiveNetworkInfo();
    if (null != info) {
        if (info.getType() == ConnectivityManager.TYPE_WIFI) {
            return true;
        }
    }
    return false;

}

From source file:Main.java

public static boolean networkStatusOK(final Context context) {
    boolean netStatus = false;

    try {/*  ww w. j a va2 s  .  c  om*/
        ConnectivityManager connectManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectManager.getActiveNetworkInfo();
        if (activeNetworkInfo != null) {
            if (activeNetworkInfo.isAvailable() && activeNetworkInfo.isConnected()) {
                netStatus = true;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return netStatus;
}

From source file:com.zirconi.huaxiaclient.SharedApplication.java

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

From source file:Main.java

public static String getNetworkState(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    String returnValue = "";
    if (null != activeNetwork) {
        if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
            returnValue = "wifi";
        else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
            returnValue = "mobile" + "_" + getNetworkType(context);
        else/*from  w w  w .  j  a v a  2 s  . c o m*/
            returnValue = "Unknown";
    } else
        returnValue = "Not connected";
    return returnValue;
}

From source file:Main.java

/**
 * checks if there is WI-FI or network connection available
 * @param currentActivity/*from w  w w .j av a2 s .c om*/
 * @return
 */
public static boolean isInternetAvailable(Context currentActivity) {
    final ConnectivityManager conectivityManager = (ConnectivityManager) currentActivity
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo networkInfo = conectivityManager.getActiveNetworkInfo();
    if (networkInfo != null) {
        if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
            if (networkInfo.isConnected()) {
                return true;
            }
        } else if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
            if (networkInfo.isConnected()) {
                return true;
            }
        }
    }

    return false;
}

From source file:Main.java

/**
 * This method checks if the Network available on the device or not.
 * /*from ww  w  .  jav a2s. c o  m*/
 * @param context
 * @return true if network available, false otherwise
 */
public static Boolean isNetworkAvailable(Context context) {
    boolean connected = false;
    final ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        connected = true;
    } else if (netInfo != null && netInfo.isConnected() && cm.getActiveNetworkInfo().isAvailable()) {
        connected = true;
    } else if (netInfo != null && netInfo.isConnected()) {
        try {
            URL url = new URL("http://www.google.com");
            HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
            urlc.setConnectTimeout(3000);
            urlc.connect();
            if (urlc.getResponseCode() == 200) {
                connected = true;
            }
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else if (cm != null) {
        final NetworkInfo[] netInfoAll = cm.getAllNetworkInfo();
        for (NetworkInfo ni : netInfoAll) {
            System.out.println("get network type :::" + ni.getTypeName());
            if ((ni.getTypeName().equalsIgnoreCase("WIFI") || ni.getTypeName().equalsIgnoreCase("MOBILE"))
                    && ni.isConnected() && ni.isAvailable()) {
                connected = true;
                if (connected) {
                    break;
                }
            }
        }
    }
    return connected;
}

From source file:com.yelinaung.android.utils.NetUtils.java

public static boolean isOnlineOrNot(Context c) {
    NetworkInfo netInfo = null;//from   ww  w.j a v a  2  s . c o m
    try {
        ConnectivityManager cm = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
        netInfo = cm.getActiveNetworkInfo();
    } catch (SecurityException e) {
        e.printStackTrace();
    }
    return netInfo != null && netInfo.isConnectedOrConnecting();
}

From source file:Main.java

static public String getMacAddress(Context context, ConnectivityManager connMananger) {
    Log.d(TAG, "getMacAddress");

    String macAddress = "";

    NetworkInfo info = connMananger.getActiveNetworkInfo();
    if (info != null && info.isConnected()) {
        int type = info.getType();

        Log.d(TAG, "connected type = " + type + " name " + info.getTypeName());

        if (type == ConnectivityManager.TYPE_WIFI) {
            WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            macAddress = wifiInfo.getMacAddress();
        } else if (type == ConnectivityManager.TYPE_ETHERNET || type == ConnectivityManager.TYPE_MOBILE) {
            String addressFileName = "/sys/class/net/eth0/address";
            File addressFile = new File(addressFileName);
            Log.d(TAG, "1");

            if (addressFile.exists()) {
                Log.d(TAG, "2");
                macAddress = readString(addressFile);
                Log.d(TAG, macAddress);/*from   w  w  w  .  j a va 2 s .  c om*/
            } else {
                addressFileName = "/sys/class/net/eth1/address";
                addressFile = new File(addressFileName);
                if (addressFile.exists()) {
                    macAddress = readString(addressFile);
                    Log.d(TAG, macAddress);
                }
            }
        } else {
            //other type;
        }
    }
    return macAddress;
}

From source file:Main.java

public static boolean networkAvailable(Context context) {

    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;
            }//from   w w  w . ja  v  a2s. c o  m
        }
    }
    return false;
}