I've researched most of the questions here but did not find a viable solution.
I'm designing an app that highly uses internet connection. I permanently need to request/receive information from a web service.
I've tried sourounding all code that uses internet with
try
{
//use of internet here
}
catch (Exception e)
{
//Alert dialog that states internet connection is down
}
but this doesn't seem to work. The exception is not catched here for some reason.
I've tried a workaround, a solution i found here. Use of:
public static boolean isOnline(Context context) {
ConnectivityManager cm =(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}
And use it like :
if (Utilities.IsOnline(context))
{
//Code that uses internet
}
What happens if the connection is lost right after the IsOnline check? How can i treat this case? Thank you!