-5

I am using Glide library to fetch images from an API. In the case of network connectivity issues, my present implementation just shows the error image. I want to display a toast message if the internet is not present.

About using services: I think it would be an overkill for a simple app to continuously check the internet. Also if images are already on the screen I don't want to raise any alarm. It is only when images are fetched, the notifications should be raised.

I tried to look into Glide's working but was unable to find a good solution.

Precisely, I want to set timeouts for Glide to fetch images. If it fails to do so a toast would be raised to inform user about low internet connectivity. Please suggest how to do so or if there is any other better way to do this.

1 Answers1

-1
public static boolean isNetConnected(Context context) {
    final ConnectivityManager mConnectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    final NetworkInfo netInfo = mConnectivityManager.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    }
    return false;

}
  • I don't like this answer for a few reasons: 1) You're using hungarian notation (mPrefix) and you're using it incorrectly. 2) You're also calling `isConnectedOrConnecting` which returns true even when you're not connected to the internet, even tho your method name is isNetConnected. Another option is `isConnected`. 3) this is a one time call and doesn't listen to changes to internet connectivity (example: disconnected, no connection) 4) you can inline that if-statement – Zun May 02 '19 at 11:09