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..
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