Class NetworkUtils



  • public class NetworkUtils
    extends java.lang.Object
    This class is a collection of utility methods for determining network state.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method and Description
      static NetworkInfo getActiveNetworkInfo(Context context)
      Gets the currently active NetworkInfo.
      static ConnectivityManager getConnectivityManager(Context context)
      Obtains an instance of the ConnectivityManager
      static boolean isConnected(Context context)
      Determines whether we have a usable connection or not.
      static boolean isMobile(Context context)
      Determines whether we are on a mobile network or not.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • isConnected

        public static boolean isConnected(Context context)
        Determines whether we have a usable connection or not.

        Specifically this calls NetworkInfo#isConnected() which determines that a usable connection is present.

        Parameters:
        context - The context which will be used to obtain an instance of ConnectivityManager
        Returns:
        true if a usable connection is present, false otherwise.
      • isMobile

        public static boolean isMobile(Context context)
        Determines whether we are on a mobile network or not.

        This checks if the type of the current active network is ConnectivityManager#TYPE_MOBILE, as reported by NetworkInfo#getType()

        Parameters:
        context - The context which will be used to obtain an instance of ConnectivityManager
        Returns:
        true if the current active connection is a mobile one, false otherwise.
      • getConnectivityManager

        public static ConnectivityManager getConnectivityManager(Context context)
        Obtains an instance of the ConnectivityManager

        This is simply a convenience method for obtaining a ConnectivityManager without having to call Context#getSystemService(String)

        Parameters:
        context - The context which will be used to obtain an instance of ConnectivityManager
        Returns:
        An instance of ConnectivityManager or null if it was not found.
      • getActiveNetworkInfo

        public static NetworkInfo getActiveNetworkInfo(Context context)
        Gets the currently active NetworkInfo.

        Please note that your application must have the Manifest.permission#ACCESS_NETWORK_STATE permission in order for this method to return actual data.

        Parameters:
        context - The context which will be used to obtain an instance of NetworkInfo
        Returns:
        The NetworkInfo representing the active network connection or null if there was an issue looking this up.