1

I want to know if there is any intent or broadcast that notifies when internet is connected/disconnected while the wifi/data network is connected all the time. I am aware of CONNECTIVITY_CHANGE but it only notifies when wifi/data is switched on/off.

Thanks

Aniruddh Parihar
  • 3,072
  • 3
  • 21
  • 39
Sohail Ahmad Khan
  • 344
  • 1
  • 3
  • 18

1 Answers1

0

In my case this block of code is working fine, you must try it

public boolean isInternetAvailable() {
try {
    InetAddress ipAddress = InetAddress.getByName("google.com"); 
    //You can replace it with your name
        return !ipAddress.equals("");

    } catch (Exception e) {
        return false;
}

}

Taha wakeel
  • 159
  • 1
  • 8
  • But how can I know where to call this code as I am not getting update.. – Sohail Ahmad Khan Nov 20 '18 at 12:32
  • Just create a service with broadcast having intent filter of network state change, so whenever network state changes call this method to check whether the internet is connected or not and return a bool according to your code scenario. – Taha wakeel Nov 20 '18 at 12:36
  • Issue is I only get broadcast if wifi/data is switched ON?OFF.. Not when it is connected and internet is available/unavailable.. – Sohail Ahmad Khan Nov 22 '18 at 05:33
  • Ok there is another work around, set intent filter as Intent.ACTION_TIME_TICK it will be triggered after every minute, so now you can check internet available/unavailable under a method which returns bool and then use that bool according to your scenario. – Taha wakeel Nov 22 '18 at 08:18