Example usage for android.net NetworkInfo getType

List of usage examples for android.net NetworkInfo getType

Introduction

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

Prototype

@Deprecated
public int getType() 

Source Link

Document

Reports the type of network to which the info in this NetworkInfo pertains.

Usage

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();
}

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 . ja v a  2  s. c  o  m*/
            } 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 isWiFiEnabled(Context context) {

    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    // i assume that connectivityManager cannot be null by default - because system service exists always \
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

    // simplified style of writing - I like this \
    return networkInfo != null && networkInfo.isConnected()
            && networkInfo.getType() == ConnectivityManager.TYPE_WIFI;
}

From source file:Main.java

public static boolean isWifiAvailable(final Context context) {

    if (context != null) {
        final ConnectivityManager connManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo[] connections = connManager.getAllNetworkInfo();
        for (NetworkInfo netInfo : connections) {
            if (netInfo != null) {
                boolean connected = netInfo.isConnected();
                int connectType = netInfo.getType();

                if (connected)
                    if (connectType == ConnectivityManager.TYPE_WIFI
                            || connectType == ConnectivityManager.TYPE_ETHERNET)
                        return true;
            }//from w w  w  .  j  a v  a2s  .c  om
        }
    }
    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/*  w ww  .  j a  v  a 2 s. com*/
            returnValue = "Unknown";
    } else
        returnValue = "Not connected";
    return returnValue;
}

From source file:Main.java

public static int getConnectedType(Context context) {
    ConnectivityManager cManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo nInfo = cManager.getActiveNetworkInfo();
    if (nInfo != null && nInfo.isAvailable()) {
        int type = nInfo.getType();
        int subType = nInfo.getSubtype();
        switch (type) {
        case ConnectivityManager.TYPE_MOBILE:
            switch (subType) {
            case 1://TelephonyManager.NETWORK_TYPE_GPRS:
            case 2://TelephonyManager.NETWORK_TYPE_EDGE:
            case 4://TelephonyManager.NETWORK_TYPE_CDMA:
            case 7://TelephonyManager.NETWORK_TYPE_1xRTT:
            case 11://TelephonyManager.NETWORK_TYPE_IDEN:
                return NETWORK_CLASS_2_G;
            case 3://TelephonyManager.NETWORK_TYPE_UMTS:
            case 5://TelephonyManager.NETWORK_TYPE_EVDO_0:
            case 6://TelephonyManager.NETWORK_TYPE_EVDO_A:
            case 8://TelephonyManager.NETWORK_TYPE_HSDPA:
            case 9://TelephonyManager.NETWORK_TYPE_HSUPA:
            case 10://TelephonyManager.NETWORK_TYPE_HSPA:
            case 12://TelephonyManager.NETWORK_TYPE_EVDO_B:
            case 14://TelephonyManager.NETWORK_TYPE_EHRPD:
            case 15://TelephonyManager.NETWORK_TYPE_HSPAP:
                return NETWORK_CLASS_3_G;
            case 13://TelephonyManager.NETWORK_TYPE_LTE:
                return NETWORK_CLASS_4_G;
            default:
                return NETWORK_CLASS_UNKNOWN;
            }/*w  w w .  java2 s.c  om*/
        case ConnectivityManager.TYPE_WIFI:

            return NETWORK_CLASS_WIFI;
        }
    }
    return NETWORK_CLASS_UNKNOWN;
}

From source file:Main.java

public static boolean ensureWifi(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo == null || !netInfo.isConnectedOrConnecting())
        return false;
    // always OK if we're on wifi
    if (netInfo.getType() == ConnectivityManager.TYPE_WIFI)
        return true;
    // check for wifi only pref
    if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean("wifiPref", true)) {
        Log.d("Podax", "Not downloading because Wifi is required and not connected");
        return false;
    }//from  ww  w  . j  av a 2 s .  c  o m
    // check for 3g data turned off
    if (!netInfo.isConnected()) {
        Log.d("Podax", "Not downloading because background data is turned off");
        return false;
    }

    return true;
}

From source file:Main.java

public static boolean isWifi(Context context) {

    ConnectivityManager mConnectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = mConnectivity.getActiveNetworkInfo();

    if (netInfo == null || !mConnectivity.getBackgroundDataSetting()) {
        return false;
    } else if (ConnectivityManager.TYPE_WIFI == netInfo.getType()) {
        return netInfo.isConnected();
    } else {//from ww  w  .  ja  v a  2  s .  c  o m
        return false;
    }

}

From source file:Main.java

public static boolean isNetworkAvailable(Context ctx) {
    int networkStatePermission = ctx.checkCallingOrSelfPermission(Manifest.permission.ACCESS_NETWORK_STATE);

    if (networkStatePermission == PackageManager.PERMISSION_GRANTED) {

        ConnectivityManager mConnectivity = (ConnectivityManager) ctx
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        // Skip if no connection, or background data disabled
        NetworkInfo info = mConnectivity.getActiveNetworkInfo();
        if (info == null) {
            return false;
        }// ww w  . j  ava 2s.  co  m
        // Only update if WiFi
        int netType = info.getType();
        // int netSubtype = info.getSubtype();
        if ((netType == ConnectivityManager.TYPE_WIFI) || (netType == ConnectivityManager.TYPE_MOBILE)) {
            return info.isConnected();
        } else {
            return false;
        }
    } else {
        return true;
    }
}

From source file:Main.java

public static boolean isWifi(Context context) {
    NetworkInfo networkInfo = ((ConnectivityManager) (context.getSystemService(Context.CONNECTIVITY_SERVICE)))
            .getActiveNetworkInfo();/*  w  w w  .  j a  va 2  s  .c o  m*/
    return networkInfo != null && networkInfo.isConnected()
            && (networkInfo.getType() == ConnectivityManager.TYPE_WIFI
                    || networkInfo.getType() == ConnectivityManager.TYPE_ETHERNET || networkInfo.getType() == 17
                    || networkInfo.getType() == -1 || networkInfo.getType() == 13
                    || networkInfo.getType() == 16);
}