Skip to content

Instantly share code, notes, and snippets.

@alexrainman
Forked from emil2k/Connectivity.java
Last active March 27, 2023 07:45

Revisions

  1. alexrainman revised this gist Nov 23, 2014. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions Connectivity.cs
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,9 @@
    using System.Threading;
    using Android.Content;
    using Android.Net;
    using Android.Telephony;
    using Java.IO;
    using Java.Net;

    namespace YourNamespace {

  2. alexrainman revised this gist Nov 23, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Connectivity.cs
    Original file line number Diff line number Diff line change
    @@ -151,7 +151,7 @@ public static bool IsHostReachable(string host)
    /*
    * It's important to note that isReachable tries ICMP ping and then TCP echo (port 7).
    * These are often closed down on HTTP servers.
    * So a prefectly good working API with a web server on port 80 will be reported as unreachable
    * So a perfectly good working API with a web server on port 80 will be reported as unreachable
    * if ICMP and TCP port 7 are filtered out!
    */

  3. alexrainman revised this gist Nov 23, 2014. 1 changed file with 43 additions and 0 deletions.
    43 changes: 43 additions & 0 deletions Connectivity.cs
    Original file line number Diff line number Diff line change
    @@ -135,5 +135,48 @@ public static bool IsConnectionFast(ConnectivityType type, NetworkType subType){
    return false;
    }
    }

    public static bool IsHostReachable(string host)
    {
    if (string.IsNullOrEmpty(host))
    return false;

    bool isReachable = true;

    Thread thread = new Thread(() =>
    {
    try {
    //isReachable = InetAddress.GetByName(host).IsReachable(2000);

    /*
    * It's important to note that isReachable tries ICMP ping and then TCP echo (port 7).
    * These are often closed down on HTTP servers.
    * So a prefectly good working API with a web server on port 80 will be reported as unreachable
    * if ICMP and TCP port 7 are filtered out!
    */

    //if (!isReachable){
    URL url = new URL("http://" + host);

    URLConnection connection = url.OpenConnection();

    //if(connection.ContentLength != -1){
    //isReachable = true;
    if(connection.ContentLength == -1){
    isReachable = false;
    }
    //}

    } catch (UnknownHostException e) {
    isReachable = false;
    } catch (IOException e) {
    isReachable = false;
    }

    });
    thread.Start();

    return isReachable;
    }
    }
    }
  4. alexrainman revised this gist Sep 23, 2014. 2 changed files with 139 additions and 122 deletions.
    139 changes: 139 additions & 0 deletions Connectivity.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,139 @@
    using Android.Content;
    using Android.Net;
    using Android.Telephony;

    namespace YourNamespace {

    /**
    * Check device's network connectivity and speed
    * @author emil http://stackoverflow.com/users/220710/emil
    *
    */
    public static class Connectivity {

    /**
    * Get the network info
    * @param context
    * @return
    */
    public static NetworkInfo GetNetworkInfo(Context context){
    ConnectivityManager cm = (ConnectivityManager) context.GetSystemService(Context.ConnectivityService);
    return cm.ActiveNetworkInfo;
    }

    /**
    * Check if there is any connectivity
    * @param context
    * @return
    */
    public static bool IsConnected(Context context){
    NetworkInfo info = Connectivity.GetNetworkInfo(context);
    return (info != null && info.IsConnected);
    }

    /**
    * Check if there is any connectivity to a Wifi network
    * @param context
    // @param type
    * @return
    */
    public static bool IsConnectedWifi(Context context){
    NetworkInfo info = Connectivity.GetNetworkInfo(context);
    return (info != null && info.IsConnected && info.Type == ConnectivityType.Wifi);
    }

    /**
    * Check if there is any connectivity to a mobile network
    * @param context
    // @param type
    * @return
    */
    public static bool IsConnectedMobile(Context context){
    NetworkInfo info = Connectivity.GetNetworkInfo(context);
    return (info != null && info.IsConnected && info.Type == ConnectivityType.Mobile);
    }

    /**
    * Check if there is fast connectivity
    * @param context
    * @return
    */
    public static bool IsConnectedFast(Context context){
    NetworkInfo info = Connectivity.GetNetworkInfo(context);
    TelephonyManager tm = TelephonyManager.FromContext (context);
    return (info != null && info.IsConnected && Connectivity.IsConnectionFast(info.Type, tm.NetworkType));
    }

    /**
    * Check if the connection is fast
    * @param type
    * @param subType
    * @return
    */
    public static bool IsConnectionFast(ConnectivityType type, NetworkType subType){
    if( type == ConnectivityType.Wifi){
    return true;
    } else if(type == ConnectivityType.Mobile){
    switch(subType){
    //case TelephonyManager.NETWORK_TYPE_1xRTT:
    case NetworkType.OneXrtt:
    return false; // ~ 50-100 kbps
    //case TelephonyManager.NETWORK_TYPE_CDMA:
    case NetworkType.Cdma:
    return false; // ~ 14-64 kbps
    //case TelephonyManager.NETWORK_TYPE_EDGE:
    case NetworkType.Edge:
    return false; // ~ 50-100 kbps
    //case TelephonyManager.NETWORK_TYPE_EVDO_0:
    case NetworkType.Evdo0:
    return true; // ~ 400-1000 kbps
    //case TelephonyManager.NETWORK_TYPE_EVDO_A:
    case NetworkType.EvdoA:
    return true; // ~ 600-1400 kbps
    //case TelephonyManager.NETWORK_TYPE_GPRS:
    case NetworkType.Gprs:
    return false; // ~ 100 kbps
    //case TelephonyManager.NETWORK_TYPE_HSDPA:
    case NetworkType.Hsdpa:
    return true; // ~ 2-14 Mbps
    //case TelephonyManager.NETWORK_TYPE_HSPA:
    case NetworkType.Hspa:
    return true; // ~ 700-1700 kbps
    //case TelephonyManager.NETWORK_TYPE_HSUPA:
    case NetworkType.Hsupa:
    return true; // ~ 1-23 Mbps
    //case TelephonyManager.NETWORK_TYPE_UMTS:
    case NetworkType.Umts:
    return true; // ~ 400-7000 kbps
    /*
    * Above API level 7, make sure to set android:targetSdkVersion
    * to appropriate level to use these
    */
    //case TelephonyManager.NETWORK_TYPE_EHRPD: // API level 11
    case NetworkType.Ehrpd:
    return true; // ~ 1-2 Mbps
    //case TelephonyManager.NETWORK_TYPE_EVDO_B: // API level 9
    case NetworkType.EvdoB:
    return true; // ~ 5 Mbps
    //case TelephonyManager.NETWORK_TYPE_HSPAP: // API level 13
    case NetworkType.Hspap:
    return true; // ~ 10-20 Mbps
    //case TelephonyManager.NETWORK_TYPE_IDEN: // API level 8
    case NetworkType.Iden:
    return false; // ~25 kbps
    //case TelephonyManager.NETWORK_TYPE_LTE: // API level 11
    case NetworkType.Lte:
    return true; // ~ 10+ Mbps
    // Unknown
    //case TelephonyManager.NETWORK_TYPE_UNKNOWN:
    case NetworkType.Unknown:
    return false;
    default:
    return false;
    }
    }else{
    return false;
    }
    }
    }
    }
    122 changes: 0 additions & 122 deletions Connectivity.java
    Original file line number Diff line number Diff line change
    @@ -1,122 +0,0 @@
    package com.emil.android.util;

    import android.content.Context;
    import android.net.ConnectivityManager;
    import android.net.NetworkInfo;
    import android.telephony.TelephonyManager;

    /**
    * Check device's network connectivity and speed
    * @author emil http://stackoverflow.com/users/220710/emil
    *
    */
    public class Connectivity {

    /**
    * Get the network info
    * @param context
    * @return
    */
    public static NetworkInfo getNetworkInfo(Context context){
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    return cm.getActiveNetworkInfo();
    }

    /**
    * Check if there is any connectivity
    * @param context
    * @return
    */
    public static boolean isConnected(Context context){
    NetworkInfo info = Connectivity.getNetworkInfo(context);
    return (info != null && info.isConnected());
    }

    /**
    * Check if there is any connectivity to a Wifi network
    * @param context
    * @param type
    * @return
    */
    public static boolean isConnectedWifi(Context context){
    NetworkInfo info = Connectivity.getNetworkInfo(context);
    return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI);
    }

    /**
    * Check if there is any connectivity to a mobile network
    * @param context
    * @param type
    * @return
    */
    public static boolean isConnectedMobile(Context context){
    NetworkInfo info = Connectivity.getNetworkInfo(context);
    return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_MOBILE);
    }

    /**
    * Check if there is fast connectivity
    * @param context
    * @return
    */
    public static boolean isConnectedFast(Context context){
    NetworkInfo info = Connectivity.getNetworkInfo(context);
    return (info != null && info.isConnected() && Connectivity.isConnectionFast(info.getType(),info.getSubtype()));
    }

    /**
    * Check if the connection is fast
    * @param type
    * @param subType
    * @return
    */
    public static boolean isConnectionFast(int type, int subType){
    if(type==ConnectivityManager.TYPE_WIFI){
    return true;
    }else if(type==ConnectivityManager.TYPE_MOBILE){
    switch(subType){
    case TelephonyManager.NETWORK_TYPE_1xRTT:
    return false; // ~ 50-100 kbps
    case TelephonyManager.NETWORK_TYPE_CDMA:
    return false; // ~ 14-64 kbps
    case TelephonyManager.NETWORK_TYPE_EDGE:
    return false; // ~ 50-100 kbps
    case TelephonyManager.NETWORK_TYPE_EVDO_0:
    return true; // ~ 400-1000 kbps
    case TelephonyManager.NETWORK_TYPE_EVDO_A:
    return true; // ~ 600-1400 kbps
    case TelephonyManager.NETWORK_TYPE_GPRS:
    return false; // ~ 100 kbps
    case TelephonyManager.NETWORK_TYPE_HSDPA:
    return true; // ~ 2-14 Mbps
    case TelephonyManager.NETWORK_TYPE_HSPA:
    return true; // ~ 700-1700 kbps
    case TelephonyManager.NETWORK_TYPE_HSUPA:
    return true; // ~ 1-23 Mbps
    case TelephonyManager.NETWORK_TYPE_UMTS:
    return true; // ~ 400-7000 kbps
    /*
    * Above API level 7, make sure to set android:targetSdkVersion
    * to appropriate level to use these
    */
    case TelephonyManager.NETWORK_TYPE_EHRPD: // API level 11
    return true; // ~ 1-2 Mbps
    case TelephonyManager.NETWORK_TYPE_EVDO_B: // API level 9
    return true; // ~ 5 Mbps
    case TelephonyManager.NETWORK_TYPE_HSPAP: // API level 13
    return true; // ~ 10-20 Mbps
    case TelephonyManager.NETWORK_TYPE_IDEN: // API level 8
    return false; // ~25 kbps
    case TelephonyManager.NETWORK_TYPE_LTE: // API level 11
    return true; // ~ 10+ Mbps
    // Unknown
    case TelephonyManager.NETWORK_TYPE_UNKNOWN:
    default:
    return false;
    }
    }else{
    return false;
    }
    }

    }
  5. @emil2k emil2k created this gist Mar 10, 2013.
    122 changes: 122 additions & 0 deletions Connectivity.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,122 @@
    package com.emil.android.util;

    import android.content.Context;
    import android.net.ConnectivityManager;
    import android.net.NetworkInfo;
    import android.telephony.TelephonyManager;

    /**
    * Check device's network connectivity and speed
    * @author emil http://stackoverflow.com/users/220710/emil
    *
    */
    public class Connectivity {

    /**
    * Get the network info
    * @param context
    * @return
    */
    public static NetworkInfo getNetworkInfo(Context context){
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    return cm.getActiveNetworkInfo();
    }

    /**
    * Check if there is any connectivity
    * @param context
    * @return
    */
    public static boolean isConnected(Context context){
    NetworkInfo info = Connectivity.getNetworkInfo(context);
    return (info != null && info.isConnected());
    }

    /**
    * Check if there is any connectivity to a Wifi network
    * @param context
    * @param type
    * @return
    */
    public static boolean isConnectedWifi(Context context){
    NetworkInfo info = Connectivity.getNetworkInfo(context);
    return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI);
    }

    /**
    * Check if there is any connectivity to a mobile network
    * @param context
    * @param type
    * @return
    */
    public static boolean isConnectedMobile(Context context){
    NetworkInfo info = Connectivity.getNetworkInfo(context);
    return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_MOBILE);
    }

    /**
    * Check if there is fast connectivity
    * @param context
    * @return
    */
    public static boolean isConnectedFast(Context context){
    NetworkInfo info = Connectivity.getNetworkInfo(context);
    return (info != null && info.isConnected() && Connectivity.isConnectionFast(info.getType(),info.getSubtype()));
    }

    /**
    * Check if the connection is fast
    * @param type
    * @param subType
    * @return
    */
    public static boolean isConnectionFast(int type, int subType){
    if(type==ConnectivityManager.TYPE_WIFI){
    return true;
    }else if(type==ConnectivityManager.TYPE_MOBILE){
    switch(subType){
    case TelephonyManager.NETWORK_TYPE_1xRTT:
    return false; // ~ 50-100 kbps
    case TelephonyManager.NETWORK_TYPE_CDMA:
    return false; // ~ 14-64 kbps
    case TelephonyManager.NETWORK_TYPE_EDGE:
    return false; // ~ 50-100 kbps
    case TelephonyManager.NETWORK_TYPE_EVDO_0:
    return true; // ~ 400-1000 kbps
    case TelephonyManager.NETWORK_TYPE_EVDO_A:
    return true; // ~ 600-1400 kbps
    case TelephonyManager.NETWORK_TYPE_GPRS:
    return false; // ~ 100 kbps
    case TelephonyManager.NETWORK_TYPE_HSDPA:
    return true; // ~ 2-14 Mbps
    case TelephonyManager.NETWORK_TYPE_HSPA:
    return true; // ~ 700-1700 kbps
    case TelephonyManager.NETWORK_TYPE_HSUPA:
    return true; // ~ 1-23 Mbps
    case TelephonyManager.NETWORK_TYPE_UMTS:
    return true; // ~ 400-7000 kbps
    /*
    * Above API level 7, make sure to set android:targetSdkVersion
    * to appropriate level to use these
    */
    case TelephonyManager.NETWORK_TYPE_EHRPD: // API level 11
    return true; // ~ 1-2 Mbps
    case TelephonyManager.NETWORK_TYPE_EVDO_B: // API level 9
    return true; // ~ 5 Mbps
    case TelephonyManager.NETWORK_TYPE_HSPAP: // API level 13
    return true; // ~ 10-20 Mbps
    case TelephonyManager.NETWORK_TYPE_IDEN: // API level 8
    return false; // ~25 kbps
    case TelephonyManager.NETWORK_TYPE_LTE: // API level 11
    return true; // ~ 10+ Mbps
    // Unknown
    case TelephonyManager.NETWORK_TYPE_UNKNOWN:
    default:
    return false;
    }
    }else{
    return false;
    }
    }

    }