Skip to content
Stanislav Koshutsky edited this page Mar 16, 2017 · 1 revision

The only class intended to use in network package is NetworkHelper. Its purpose is mostly the same to Google's Network Manager but this class monitors network state and also it not only checks if any network connection is available but if given host is available too (to check if there is an active WiFi with Internet, not just LAN over WiFi). FOA you need to initialize it in several ways but all of it requires context. This class stores application context in its static field (since app context is singleton its ok to keep it this way).

public static void init(final Context context)  
public static void init(final Context context, final String hostToCheck)  
public static void init(final Context context, final String hostToCheck, final String httpMethod)  
public static void init(final Context context, final String hostToCheck, final int hostCheckFrequencyLimit)  

provide host if you want to check its availability via current network connection.
Provide desired HTTP Method to use. Usually it is GET or POST and it is HEAD by default.
hostCheckFrequencyLimit - is a limitation of how often to check the host.
The way this helper works it checks host availability every time network connection changes using broadcast receiver. However, when app goes background it needs this monitor no more and thus you could use method:

public static void unregisterReceiver()  

After that when app goes foreground again you should register network broadcast receiver once again using the following method:

public static void registerReceiver()  
public static void registerReceiver(Context context)  

If you need to get mentioned receiver you could use methods:

public static NetworkStateReceiver getReceiver(final Context context)  
public static NetworkStateReceiver getReceiver(final Context context, final String host) {

If you want to set or change the host address:

public static void setHostToCheck(final String hostToCheck) {

Checks if currently device has any network connection without ensuring it has Internet access. And additionally it starts a host check task to ping the host public static boolean isNetworkAvailable()

To check if desired HTTP-method (to check host) supported, which are: OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE

public static boolean isPermittedHTTPMethod(final String method)  

Checks host by connection to it using HttpURLConnection and desired HTTP-method (HEAD by default). Method should not be run on a Main/UIThread otherwise exception (Networking on main thread) will be thrown.

public static boolean isHostReachable(final String hostUrl)  

all what this method does - it connects to host and immediately disconnects and checks the HTTP-code. If its something greater than 0 (ie 200, 500, 404) host is available. No matter which code returned by server, it is important that server returns something at all, which means host is available.
Note: If host is HTTP-secured (like https://blahblah.com) then "trust any SSL certificate" rule will be applied.

The following method is the same with only difference - isHostReachable is separated to another thread, so it works asynchronous and thats why it requires callback by implementing interface ICheckIfHostResponds.

public static void checkIfHostResponds(final String hostUrl, final ICheckIfHostResponds callback)  

Interface to deliver result of checkIfHostResponds()

public interface ICheckIfHostResponds {
    void doesHostRespond(boolean doestIt);
}

Method checks if currently device has any network connection without ensuring it has Internet access.

public static boolean isActiveNetworkConnectionAvailable(final Context context)  
public static boolean isActiveNetworkConnectionAvailable()  

Method checks if any (INTERNET_OVER_WIFI,INTERNET_OVER_MOBILE, INTERNET_OVER_OTHER) network connection is available.

  1. issue1: assumes the network connection is an Internet connection

  2. issue2: fails if a connection requires a login/password (like hotels WiFi)

    public static boolean isAnyNetworkConnectionAvailable(final Context context)

Method checks if an active network (Internet) connection works via WiFi
issue1: assumes the WiFi connection is an Internet connection
issue2: fails if a WiFi connection requires a login/password

public static boolean isActiveConnectionViaWiFi()  

Method checks if an network (Internet) connection works via WiFi or Ethernet (like TV-box)
issue1: assumes the WiFi connection is an Internet connection
issue2: fails if a WiFi connection requires a login/password

public static boolean isActiveConnectionViaWiFiOrEthernet()  

Method checks if WIFI network connection is available.

public static boolean isNetworkAvailableViaWiFi()  

Clone this wiki locally