0

I want to check internet connection in my android app. I searched on it, what I got was like it only check whether the phone is connected to the network or not, It does not check the real internet connection. Please help..?

Rahul Yadav
  • 161
  • 3
  • 11

1 Answers1

2

I suggest you to make a Utility class in which you can use this function to check if you are connected to internet or not. Make sure you have the appropriate permission in your manifest as well

public static boolean isNetworkAvailable(Context context) {
        if (context == null) {
            return false;
        }
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    }

Alternate : I suggest you to use this library https://github.com/Blankj/AndroidUtilCode which have many utility classes which we require in our daily use. No need to reinvent wheel every time

MRX
  • 1,400
  • 3
  • 12
  • 32
  • its only check whether it is connected to network or not. It does not check the actual network connection. – Rahul Yadav Jun 27 '17 at 07:07
  • By actual connection you mean which Network its connected to : Like 3g, 4g or wifi ? M I getting you correct ? – MRX Jun 27 '17 at 07:08
  • https://github.com/Blankj/AndroidUtilCode/blob/master/utilcode/src/main/java/com/blankj/utilcode/util/NetworkUtils.java – MRX Jun 27 '17 at 07:09
  • I mean its only check the phone is connected to connected to wifi router r not it does not check whether the router is connected to internet or not. – Rahul Yadav Jun 27 '17 at 11:14
  • Nope that will not check your router internet connectivity but what you can do is you can ping an address if its connected to wifi and check the result – MRX Jun 27 '17 at 11:16