Monday, February 23, 2015

Check network connectivity in android

Before providing any activity like accessing an application, loading emails, chatting, a user need to check whether he or she is connected to a network or not. For doing the same ConnectivityManager is used. It check whether a user is connected to a network or not and if the user is not connected than show an error message.



public static boolean haveNetworkConnection(Context con) {
   
  ConnectivityManager connectivity = (ConnectivityManager) con.getSystemService(Context.CONNECTIVITY_SERVICE);


        if (connectivity != null)
        {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null)
                for (int i = 0; i < info.length; i++)
                    if (info[i].getState() == NetworkInfo.State.CONNECTED)
                    {
                        return true;
                    }

        }
        return false;
  }




it will return true .,if device is connected with a network otherwise return false..

0 comments:

Post a Comment

Don't lose faith when you see others receive answers to their prayers

An elephant and a dog became pregnant at same time. Three months down the line the dog gave birth to six puppies. Six months later the dog...

 

G-Expo Template by Ipietoon Cute Blog Design