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:li.barter.utils.Utils.java

/**
 * Reads the network info from service and sets up the singleton
 *///from ww w .j  a v  a2  s .  co m
public static void setupNetworkInfo(final Context context) {

    final ConnectivityManager connManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo activeNetwork = connManager.getActiveNetworkInfo();
    if (activeNetwork != null) {
        DeviceInfo.INSTANCE.setNetworkConnected(activeNetwork.isConnectedOrConnecting());
        DeviceInfo.INSTANCE.setCurrentNetworkType(activeNetwork.getType());
    } else {
        DeviceInfo.INSTANCE.setNetworkConnected(false);
        DeviceInfo.INSTANCE.setCurrentNetworkType(ConnectivityManager.TYPE_DUMMY);
    }

    Logger.d(TAG, "Network State Updated Connected: %b Type: %d", DeviceInfo.INSTANCE.isNetworkConnected(),
            DeviceInfo.INSTANCE.getCurrentNetworkType());
}

From source file:org.deviceconnect.android.manager.core.util.DConnectUtil.java

/**
 * Gets the ip address.//w w  w . j  a v  a  2 s  . c  om
 *
 * @param context Context of application
 * @return Returns ip address
 */
public static String getIPAddress(final Context context) {
    Context appContext = context.getApplicationContext();
    WifiManager wifiManager = (WifiManager) appContext.getSystemService(Context.WIFI_SERVICE);
    ConnectivityManager cManager = (ConnectivityManager) appContext
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo network = cManager.getActiveNetworkInfo();
    String en0Ip = null;
    if (network != null) {
        switch (network.getType()) {
        case ConnectivityManager.TYPE_ETHERNET:
            try {
                for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                        .hasMoreElements();) {
                    NetworkInterface intf = en.nextElement();
                    for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr
                            .hasMoreElements();) {
                        InetAddress inetAddress = enumIpAddr.nextElement();
                        if (inetAddress instanceof Inet4Address
                                && !inetAddress.getHostAddress().equals("127.0.0.1")) {
                            en0Ip = inetAddress.getHostAddress();
                            break;
                        }
                    }
                }
            } catch (SocketException e) {
                Log.e("DConnectUtil", "Get Ethernet IP Error", e);
            }
        }
    }

    if (en0Ip != null) {
        return en0Ip;
    } else {
        int ipAddress = wifiManager.getConnectionInfo().getIpAddress();
        return String.format(Locale.getDefault(), "%d.%d.%d.%d", (ipAddress & 0xff), (ipAddress >> 8 & 0xff),
                (ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff));
    }
}

From source file:com.jtechme.apphub.UpdateService.java

/**
 * If we are to update the repos only on wifi, make sure that connection is active
 *//*from  w  ww  . j av a 2 s. c om*/
public static boolean isNetworkAvailableForUpdate(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    // this could be cellular or wifi
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (activeNetwork == null)
        return false;

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    if (activeNetwork.getType() != ConnectivityManager.TYPE_WIFI
            && prefs.getBoolean(Preferences.PREF_UPD_WIFI_ONLY, false)) {
        Log.i(TAG, "Skipping update - wifi not available");
        return false;
    }
    return activeNetwork.isConnectedOrConnecting();
}

From source file:ru.dublgis.androidhelpers.DesktopUtils.java

public static int getNetworkType(final Context ctx) {
    try {/*from   w  ww.j a  v a  2 s .co  m*/
        ConnectivityManager cmgr = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cmgr == null)
            return -1;
        NetworkInfo netinfo = cmgr.getActiveNetworkInfo();
        if (netinfo == null)
            return -1;
        return netinfo.getType();
    } catch (final Throwable e) {
        Log.e(TAG, "GetNetworkType exception: ", e);
    }
    return -1;
}

From source file:com.bbxiaoqu.api.util.Utils.java

/**
 * Proxy//from w  ww  .  ja  va  2 s.c om
 */
public static HttpHost detectProxy(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni = cm.getActiveNetworkInfo();
    if (ni != null && ni.isAvailable() && ni.getType() == ConnectivityManager.TYPE_MOBILE) {
        String proxyHost = android.net.Proxy.getDefaultHost();
        int port = android.net.Proxy.getDefaultPort();
        if (proxyHost != null) {
            return new HttpHost(proxyHost, port, "http");
        }
    }
    return null;
}

From source file:io.github.hidroh.materialistic.AppUtils.java

public static boolean isOnWiFi(Context context) {
    NetworkInfo activeNetwork = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE))
            .getActiveNetworkInfo();//from   www.  j  av  a  2s  .  com
    return activeNetwork != null && activeNetwork.isConnectedOrConnecting()
            && activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;
}

From source file:edu.mit.mobile.android.locast.data.CastMedia.java

/**
 * Attempts to guess if the video player should show a high quality version of the video or a
 * lower bitrate version.//from  www.  j av a  2s  .c  o  m
 *
 * @return true if it seems as though playing high-quality would be expensive or wouldn't work
 */
public static boolean shouldShowLowQuality(Context context) {
    final ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    final boolean metered = ConnectivityManagerCompat.isActiveNetworkMetered(cm);
    final NetworkInfo net = cm.getActiveNetworkInfo();

    if (metered || net == null || net.isRoaming()) {
        return true;
    }

    // this is because these devices tend to not be able to be powerful enough to show the full
    // res video
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
        return true;
    }

    final int type = net.getType();

    switch (type) {
    // generally these are fast and cheap/free
    case ConnectivityManager.TYPE_WIFI:
    case ConnectivityManager.TYPE_ETHERNET:
    case ConnectivityManager.TYPE_WIMAX:
        return false;

    default:
        return true;
    }
}

From source file:org.restcomm.app.utillib.DataObjects.PhoneState.java

public static boolean isNetworkWifi(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivityManager != null) {
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        if (networkInfo != null) {
            int wifiState = networkInfo.getType();
            return (wifiState == ConnectivityManager.TYPE_WIFI);
        }/*from   w w  w . ja  v  a 2s.c  o m*/
    }
    return false;
}

From source file:org.restcomm.app.utillib.DataObjects.PhoneState.java

public static int ActiveConnection(Context context) {
    try {/*ww  w  .j  a v  a 2s  . c o m*/
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager != null) {
            NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
            if (networkInfo != null) {
                int wifiState = networkInfo.getType();
                if (wifiState == ConnectivityManager.TYPE_WIFI)
                    return 10;
                else if (wifiState == PhoneState.TYPE_WIMAX)
                    return 11;
                else if (wifiState == PhoneState.TYPE_ETHERNET)
                    return 12;
                else if (wifiState == PhoneState.TYPE_BLUETOOTH)
                    return 13;
            }
        }
    } catch (Exception e) {
        return -1;
    }
    return 0;
}

From source file:com.adam.aslfms.util.Util.java

public static NetworkStatus checkForOkNetwork(Context ctx) {

    AppSettings settings = new AppSettings(ctx);
    PowerOptions powerOptions = checkPower(ctx);

    NetworkOptions networkOptions = settings.getNetworkOptions(powerOptions);
    boolean roaming = settings.getSubmitOnRoaming(powerOptions);

    ConnectivityManager connectivityManager = (ConnectivityManager) ctx
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
    if (netInfo == null) {
        Log.d(TAG, "netInfo == null");
    }//from   w ww  . ja v  a  2  s  . c o  m
    if (netInfo != null) {
        Log.d(TAG, "conn: " + netInfo.isConnected() + " : " + netInfo.toString());
    }

    if (netInfo == null || !netInfo.isConnected()) {
        return NetworkStatus.DISCONNECTED;
    }

    if (netInfo.isRoaming() && !roaming) {
        return NetworkStatus.UNFIT;
    }

    int netType = netInfo.getType();
    int netSubType = netInfo.getSubtype();

    Log.d(TAG, "netType: " + netType);
    Log.d(TAG, "netSubType: " + netSubType);

    if (networkOptions.isNetworkTypeForbidden(netType)) {
        Log.d(TAG, "Network type forbidden");
        return NetworkStatus.UNFIT;
    }
    if (networkOptions.isNetworkSubTypeForbidden(netType, netSubType)) {
        Log.d(TAG, "Network sub type forbidden");
        return NetworkStatus.UNFIT;
    }

    return NetworkStatus.OK;
}