0

I am working on an android project, in which I need a way to actively check internet connection.

The application is supposed to show red dot whenever it can't connect to the internet (though it is connected to the WiFi), and green dot if it can connect.

The code I have now is just showing green when connected to WiFi (weather or not the WiFi has access to internet) or else red, and also I want to have this check every 10 sec (weather it can connect to internet though it is connected to WiFi).

I am using this code:

  connectivityManager= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            ImageView NetworkStatus = (ImageView)viewGroup.findViewById(R.id.networkAvailability);
            NetworkRequest.Builder builder = new NetworkRequest.Builder();

            connectivityManager.registerNetworkCallback(
                    builder.build(),
                    new ConnectivityManager.NetworkCallback() {
                        @Override
                        public void onAvailable(Network network) {

                                if(isOnline()){
                                    NetworkStatus.setImageResource(R.drawable.network_circle_on);
                                }
                                else
                                    {
                                        NetworkStatus.setImageResource(R.drawable.network_circle_off);

                                    }
                        }
                        @Override
                        public void onLost(Network network) {

                            NetworkStatus.setImageResource(R.drawable.network_circle_off);
                        }
                    }

            );

The isOnline() method is this:

public boolean isOnline() {
    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; }
}

And, also I need this check to happen every 10 sec. How should I do this?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Sonic_
  • 347
  • 3
  • 9

1 Answers1

0

I solved my problem using a ScheduledExecutorService.

scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() {
                public void run() {

                    connectivityManager.registerNetworkCallback(
                            builder.build(),
                            new ConnectivityManager.NetworkCallback() {
                                @Override
                                public void onAvailable(Network network) {

                                    if(isOnline()){
                                        NetworkStatus.setImageResource(R.drawable.network_circle_on);
                                    }
                                    else
                                    {
                                        NetworkStatus.setImageResource(R.drawable.network_circle_off);

                                    }
                                }

                                @Override
                                public void onLost(Network network) {

                                    NetworkStatus.setImageResource(R.drawable.network_circle_off);
                                }
                            }

                    );

                }
            }, 0, 4, TimeUnit.SECONDS);
Sonic_
  • 347
  • 3
  • 9