Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically check availibilty of internet connection in Android?

Tags:

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();"

like image 793
Fahad Avatar asked Dec 25 '10 16:12

Fahad


People also ask

How can you check if you are connected to the internet before any network operations in Android?

Checking Network ConnectiongetSystemService(Context. CONNECTIVITY_SERVICE); Once you instantiate the object of ConnectivityManager class, you can use getAllNetworkInfo method to get the information of all the networks. This method returns an array of NetworkInfo.

How do I check my current network?

Select the Start button, then type settings. Select Settings > Network & internet. The status of your network connection will appear at the top. Windows 10 lets you quickly check your network connection status.


1 Answers

The method I implemented for myself:

/*  * isOnline - Check if there is a NetworkConnection  * @return boolean  */ protected boolean isOnline() {     ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);     NetworkInfo netInfo = cm.getActiveNetworkInfo();     if (netInfo != null && netInfo.isConnected()) {         return true;     } else {         return false;     } } 

Be aware of that this is a NetworkConnection-Check. If there is a NetworkConnection it doesn't have to be a InternetConnection.

like image 131
Christoph Haefner Avatar answered Sep 23 '22 17:09

Christoph Haefner