Is there a listener to get notified when internet connection is available?

Short: Is there some listener to get notified when internet connection becomes available for the app?

Long:

I'm adding some feature to a data field that fetches things from a web api. I want to support as many devices as possible, so I make the web requests from the background app. This already works, when everything is OK, but sometimes there's no internet. Currently I register for the temporal even when I need it, and if there's no internet then it'll return something like responseCode: -104. So currently all I can do is to register for another temporal event 5 minutes later and hope that there will be internet.

So I thought I could be smarter. But the only way I could make it now is to check System.getDeviceSettings().connectionAvailable and only call Background.registerForTemporalEvent() if there's internet NOW (even when I want to trigger the event to be a few minutes in the future). This would not be a big problem, but because I can only trigger the background process every 5 minutes, this doesn't really work.

I couldn't find any listener that I could register for getting events when the internet becomes available. Is there no way to know it besides "polling" the device settings?

I am thinking about some smarter way to do it, but it would need me to build some framework around this... Like:

In the foreground process "register" for temporal event by saving the moment when I want the temporal event to run at the earliest, and set a timer, and when the timer triggers check for the internet, and if available, then trigger the background immediately, if not then reschedule the timer for later and re-check every X minutes.

  • There isn't anything like you are asking about.

    But at the same time, there are errors other than -104 you'll see in the callback to makeWebRequest you'll want to handle.  For example, you may have a connection to the phone, but the phone may not have a data connection, or the specific server you are using could be down, so you want to handle anything other that a 200 response code.

    What I do, is in the makeWebRequest callback, if I get a 200, I return the data (a dictionary) I get back, but for non-200 callbacks, I return the responseCode.  Then in onBackgroundData the main app can check what it sees with "Instanceof Number", and if it's a Number, there was an error, and the main app handles it - maybe it displays the error or shortens the time for the next temporal event - it's up to you.

    On some devices/app types,  you can skip the whole background service and just do the makeWebRequest from the main app, where you can do it as often as you want and not be limited to the "at most, every 5 minutes" for a temporal event

  • But the only way I could make it now is to check System.getDeviceSettings().connectionAvailable and only call Background.registerForTemporalEvent() if there's internet NOW

    As discussed previously, DeviceSettings.connectionAvailable doesn't necessarily tell you that internet connectivity is available.

    It's a bit of an extreme edge case [*], but your Garmin device could be connected to your phone / Connect via bluetooth, but your phone itself may not have internet connectivity. In this case, connectionAvailable would still be true.

    I only bring it up because it's come up in the forums before, not because I think it makes a huge difference here. (Someone wanted a way to know if their *phone* had internet access.)

    [*] then again it's not totally unrealistic - you could be in a subway or airplane, for example. (The subway where I live doesn't have cell connectivity :/)