There is no possible way to programmatically obtain the information you are seeking.
If there is a "problem with the internet connection", such as is very common with cellular data connections that've just entered a dead zone, you will see the cellular link remain established for several seconds until it times out. During this waiting period while the link layer still thinks it's connected but no packets can get through, the only thing you can do is start waiting yourself -- and pick an arbitrary timeout value less than or equal to the timeout value of the link layer.
Since timeout-based connectivity is based on waiting an arbitrary period of time and then making a decision about whether you think the packets are going to arrive or not, there is no general solution to determine -- without blocking -- if the connection will succeed. After all, just because a packet sent immediately prior got through, doesn't mean that the connection couldn't be killed immediately after.
I disagree with the test that tries to fetch a small resource from a URL somewhere. That test is problematic because it involves blocking, which will slow down your program. If the packet your program actually tries to send out is able to get through before the link layer or protocol layer times it out, you'll get results. If the packet doesn't make it through, you won't get results. It's that simple.
Internet connectivity is a "transient" phenomenon: it is impossible to be sure that it is available ahead of time. It's there one instant and gone the next. Like the existence of a file on disk. Instead of trying to change the flow of your program's logic depending on internet connectivity, you should instead just react to situations where connectivity is demonstrably not available, such as the packet didn't get through, or you didn't get a response, or the network interface declares that it is down (which in essence is what the Android API you're calling is doing). Handle the exception; don't try to determine whether it's up and then do something.
In conclusion, the very concept of an isNetworkConnected()
function is mostly meaningless, except for the simple test of whether the link layer thinks it's connected or not (that much, at least, can save you time and effort, because no link layer obviously implies that the connection is known not to be available, whereas all other failure states involve unknowns until after the problem is detected experimentally.) The problem is that even the most reliable and strict tests for network availability within the implementation of isNetworkConnected()
cannot atomically guarantee that, between the time the isNetworkConnected()
function returns and the time that you start trying to upload/download real data, the result hasn't changed. It's basically a race condition that cannot possibly be solved, because you don't control the user's internet infrastructure, so you can't atomically "lock" it into a working state, perform your work, and release the lock. Can't be done.