I have this scenario:
Works perfectly
You can check internet connection of device by using isConnected method of this class. Then you dismiss progress dialog when there is not internet connection
public class InternetConnectionDetector
{
private Context context;
public InternetConnectionDetector(Context context)
{
this.context = context;
}
public boolean isConnected()
{
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//There are changes after apk lollipop while detecting internet connection:
//if user's os newer then lollipop:
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
Network[] networks = connectivityManager.getAllNetworks();
NetworkInfo networkInfo;
for(Network mNetwork : networks)
{
networkInfo = connectivityManager.getNetworkInfo(mNetwork);
if(networkInfo.getState().equals(NetworkInfo.State.CONNECTED))
{
return true;
}
}
}
//if user's os older then lollipop:
else
{
//getAllNetworkInfo method work for only before API 19:
NetworkInfo[] info = connectivityManager.getAllNetworkInfo();
if(info != null)
{
for(NetworkInfo anInfo : info)
{
if(anInfo.getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
}
}
}
return false;
}
}