Karlo A. López
Karlo A. López

Reputation: 2668

Best way to check internet connection in background?

I want to check the internet connection, not only if the user is connected to a network, I want to check that this network actually is connected to the internet, im using the following code:

public boolean isInternetAvailable() {
        try {
            InetAddress ipAddr = InetAddress.getByName("google.com"); 

            if (ipAddr.equals("")) {
                return false;
            } else {
                return true;
            }

        } catch (Exception e) {
            return false;
        }

    }

Got it from this Answer thanks by the way.

This is not working as I would liko to, always return false even if Im in a correct network,I would like to know how to run this in the background and show a progress indicator which tells the user the progress of the conection test.

I need help with this cause my app works 80% with JSON parse and I rather prefer to check the connection once at the begginnign instead of checking every time I parse a JSON. Thanks.

Upvotes: 0

Views: 443

Answers (1)

tamir_sagi
tamir_sagi

Reputation: 165

If I understood you correctly,

I would use progress bar (javaFX) and use a single thread which runs your function and updates the progress bar correspondingly

ProgressBar and Background Processes

Android:Using Thread and Progress bar

Upvotes: 1

Related Questions