0

Is there any way to find out that android device is connected to the Internet or not. I use this function:

    public boolean isDeviceOnline() {     
        ConnectivityManager cm =
        (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();     
        return netInfo != null && netInfo.isConnectedOrConnecting(); 
    }

but this function return true if device connected to a WiFi network that doesn't include internet access or requires browser-based authentication and I want return false in this situation.

Amir
  • 37
  • 14
  • 2
    Try to ping google.com. – Andrii Omelchenko Aug 21 '17 at 13:14
  • "but this function return true if device connected to a WiFi network that doesn't include internet access or requires browser-based authentication and I want return false in this situation" -- then make an Internet request (e.g., HTTP request) of the server that interests you. There is no concept of "connected to the Internet" -- the only thing that you can determine is whether you can contact the server that you want. – CommonsWare Aug 21 '17 at 13:40

3 Answers3

2

try this

public class ConnectionDetector {

private Context _context;

public ConnectionDetector(Context context) {
    this._context = context;
}

public boolean isInternetAvailble() {
    return isConnectingToInternet() || isConnectingToWifi();
}

private boolean isConnectingToInternet() {
    ConnectivityManager connectivity = (ConnectivityManager) _context
            .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;
}

private boolean isConnectingToWifi() {
    ConnectivityManager connManager = (ConnectivityManager) _context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo mWifi = connManager
            .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (mWifi != null) {
        if (mWifi.getState() == NetworkInfo.State.CONNECTED)
            return true;
    }
    return false;
} }

declare like this

ConnectionDetector ConnectionDetector = new ConnectionDetector(
            context.getApplicationContext());

use like is

ConnectionDetector.isInternetAvailble()
  • 1
    Correct me if I'm wrong, but doesn't this only check if you have a network connection? What if you need to know if that network connection has internet access? Is there a better way that trying to go to some web address? – stackunderflows Aug 01 '18 at 17:10
0

just try this method >

public boolean isNetworkConnected() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

    return cm.getActiveNetworkInfo() != null; // return true =(connected),false=(not connected)
}
Tamzid
  • 89
  • 2
  • 11
0

Change this:

return netInfo != null && netInfo.isConnectedOrConnecting();

To:

return netInfo != null && netInfo.isConnected();

Then call it like

if(!isDeviceOnline()) {
    System.out.println("Not connected");
}
Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Julfikar
  • 1,353
  • 2
  • 18
  • 35