0

My app works in offline mode also and it works fine when internet is available. I am using the following code to check internet connection.

ConnectivityManagercm = (ConnectivityManager) ConnectivityClass.getInstance().getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
return activeNetwork != null && activeNetwork.isConnectedOrConnecting();

Problem is if we connected WiFi network that doesn't include internet access or requires browser-based authentication it still returns true. It should returns false if internet is not available. I need to check internet connection rather than mobile network connection status.

minSdkVersion 15

sagar
  • 76
  • 8
  • 1
    Possible duplicate of [How to check internet access on Android? InetAddress never times out](https://stackoverflow.com/questions/1560788/how-to-check-internet-access-on-android-inetaddress-never-times-out) – Itamar Kerbel Oct 27 '18 at 06:35

3 Answers3

1

You can ping URL. Then u can know the internet is exactly available.

public Boolean isInternetAvailable() {
    try {
        Process process = java.lang.Runtime.getRuntime().exec("ping -c 1 www.google.com");

        boolean avaiablility = (p1.waitFor()==0);
        return avaiablility;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return false;
}
PushpikaWan
  • 2,437
  • 3
  • 14
  • 23
0

hopefully using this code, u can check network connection

public class InternetCheckerTask extends AsyncTask<Void, Void, Void> {

private OnInternetCheckListener listener;
private int authStatus;
private boolean running;

public InternetCheckerTask(OnInternetCheckListener listener) {
    this.listener = listener;
}

@Override
protected Void doInBackground(Void... voids) {
    running = true;
    if(isInternetReallyAvailable()) {
        authStatus = AuthConstants.CONNECTED;
    } else {
        authStatus = AuthConstants.NOT_CONNECTED;
    }
    return null;
}

@Override
protected void onPostExecute(Void aVoid) {
    if(listener != null) {
        listener.onResponse(authStatus);
    }
    running = false;
}

// should not be called from ui thread, can be called only from background service
private boolean isInternetReallyAvailable() {
    ConnectivityManager cm = (ConnectivityManager) App.getInstance().getBaseContext().getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();

    if(activeNetwork == null || !activeNetwork.isAvailable() || !activeNetwork.isConnected()) {
        return false;
    } else {
        // TCP/HTTP/DNS (depending on the port, 53=DNS, 80=HTTP, etc.)
        try {
            int timeoutMs = 1500;
            Socket sock = new Socket();
            SocketAddress sockaddr = new InetSocketAddress("8.8.8.8", 53);

            sock.connect(sockaddr, timeoutMs);
            sock.close();

            return true;
        } catch (IOException e) {
            return false;
        }
    }
}

public boolean isRunning() {
    return running;
}

public interface OnInternetCheckListener {
    void onResponse(int status);
}

}

create a class & define the following constant value

public class AuthConstants {
    public static final int CONNECTED = 1;
    public static final int NOT_CONNECTED = 2;
}

and this is how you should use this feature from wherever you need

new InternetCheckerTask(new InternetCheckerTask.OnInternetCheckListener() {
        @Override
        public void onResponse(int status) {
            switch (status) {
                case AuthConstants.CONNECTED:
                    // connection is available
                    break;
                default:
                    // no connection
                    break;
            }
        }
    }).execute();
0
public static boolean isOnline(Context ctx) {
    ConnectivityManager cm = (ConnectivityManager) ctx
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm != null
            ? cm.getActiveNetworkInfo()
            : null;
    return netInfo != null && netInfo.isConnectedOrConnecting();
}
  • Check my answer to handle network change between wifi and mobile data.
Khaled Lela
  • 7,831
  • 6
  • 45
  • 73