• Have Any Question?
  • contact@eitworld.com

Network Status





    • Android provides the facility to check that our device is connected to any network or not. To check the network status, we use ConnectivityManager class.
    • So first of all we have to create the object of ConnectivityManager class. After that we have to call the getSystemService() method using the object of ConnectivityManager class.
    • Here this method takes one argument, pass the CONNECTIVITY_SERVICE context.
    • This method returns an object and it is converted and assign to the object of the ConnectivityManager class.
    • Now on this object, we have to call the isConnected() method. This method returns true if our device is connected to network otherwise return false that tells our device is not connected to network.
    • Following are the demo example:
ConnectivityManager obj;
obj =(ConnectivityManager) (this.getSystemService(context.CONNECTIVITY_SERVICE);
if(obj.isConnected())
{
Toast t1 = new Toast();
t1.makeText(context, “Connected to internet”,Toast.LENGTH_SHORT);
t1.show();
}
else
{
Toast t2 = new Toast();
t2.makeText(context, ” Not connected to internet”,Toast.LENGTH_SHORT);
t2.show();
}
  • In the above example, if device is connected to internet then toast will display the message “Connected to internet” and if our device is not connected to internet then message “Not connected to internet” will show in the form of toast.


Real Time Web Analytics