I want to check programmatically whether there is an internet connection in Android phone/emulator. So that once I am sure that an internet connection is present then I'll make a call to the internet.
So its like "Hey emulator! If you have an internet connection, then please open this page, else doSomeThingElse();"
as for the question title , you want to check the internet access ,
so the fastest way and it is efficient at least for now ,
thnx to levit based on his answer https://stackoverflow.com/a/27312494/3818437
That's it! Yes that short, yes it is fast, no it does not need to run in background, no you don't need root privileges.
i have been using these two methods to check internet status sometimes https protocol doesn't work try http protocol
Being connected to a network does not guarantee internet connectivity.
You might be connected to your home's wifi, but you might not have internet connection. Or, you might be in a restaurant, where you can be connected to the wifi, but still need password for the hotspot in order to use the internet. In such cases the above methods will return true, yet the device wont have internet, and if not surrounded with the right try and catches, the app might crash.
Bottom line, network connectivity, does not mean internet connectivity, it merely means that your device's wireless hardware can connect to another host and both can make a connection.
Below is a method that can check if the device can connect to a website and returns an answer accordingly.
The method I implemented for myself:
Be aware of that this is a NetworkConnection-Check. If there is a NetworkConnection it doesn't have to be a InternetConnection.
Try this:
Because you are connected to a network does not guaruntee that you have internet, I have gone to just making my calls to the internet inside of a try catch block. Then catch the UnknownHostException to handle no internet.