Sunday, 25 January 2015

CONNECTIVITY MANAGER CLASS.


A class that answers queries about the state of network connectivity and notifies applications when the network state change.

The responsibilities of this class are:

1. Monitor network connections (Wi-Fi, GPRS ,UMTC,...)

2.Send broadcast intent when network connectivity changes.

3.Attempt to connect to another network if the current network is lost.

4.Provide an API that allows applications to query state of available networks.


Context.getSystemService(Context.CONNECTIVITY_SERVICE) is called to get an instance of the Connectivity manager class.

PUBLIC METHODS IN THE CONNECTIVITY MANAGER CLASS.
  • NetworkInfo getActiveNetworkInfo()
Returns details about the current active default data network.
  • NetworkInfo[]  getAllNetworkInfo()
Returns connection status information about all network types supported by the device.
  • NetworkInfo  getNetworkInfo(int networkType)
Returns connection status information about a particular network type.
  • int  getNetworkPreference()
Retrieves the current preferred network type.

  •  boolean  isActiveNetworkMetered()
Returns if the currently active data network is metered.

  •  static boolean  isNetworkTypeValid(int networkType)
Tests if a given integer represents a valid network type.

  • boolean  requestRouteToHost(int networkType, int hostAddress)
Ensure that a network route exists to deliver traffic to the specified host via the specified network interface.

  • void  setNetworkPreference(int preference)
Specifies the preferred network type.

  • int  startUsingNetworkFeature(int networkType, String feature)

Tells the underlying networking system that the caller wants to begin using the named feature.

  •  int stopUsingNetworkFeature(int networkType, String feature)
Tells the underlying networking system that the caller is finished using the named feature


CHECKING NETWORK AVAILABILITY SOURCE CODE.




private boolean isNetworkAvailable() {
      ConnectivityManager connectivityManager   = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
      NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
     return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}