Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect whether there is an Internet connection available on Android [duplicate]

Possible Duplicate:
How to check internet access on Android? InetAddress never timeouts

I need to detect whether the Android device is connected to the Internet.

The NetworkInfo class provides a non-static method isAvailable() that sounds perfect.

Problem is that:

NetworkInfo ni = new NetworkInfo(); if (!ni.isAvailable()) {     // do something } 

throws this error:

The constructor NetworkInfo is not visible. 

Safe bet is there is another class that returns a NetworkInfo object. But I don't know which.

  1. How to get the above snippet of code to work?
  2. How could I have found myself the information I needed in the online documentation?
  3. Can you suggest a better way for this type of detection?
like image 308
Dan Avatar asked Nov 21 '10 16:11

Dan


People also ask

How do you check if a device is connected to internet?

Open the Google Wifi app . Devices. The numbers next to "Devices" represent your total Internet (WAN) traffic to and from your network. Under each device, you can view how much data each device has downloaded and uploaded.

How do I check network availability?

On an Android phone or tablet, open the Settings app and go to Network Connections to manage Wi-Fi, Bluetooth, and other networks such as mobile network and VPNs. Some newer versions call this Network & internet.


2 Answers

The getActiveNetworkInfo() method of ConnectivityManager returns a NetworkInfo instance representing the first connected network interface it can find or null if none of the interfaces are connected. Checking if this method returns null should be enough to tell if an internet connection is available or not.

private boolean isNetworkAvailable() {     ConnectivityManager connectivityManager            = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);     NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();     return activeNetworkInfo != null && activeNetworkInfo.isConnected(); } 

You will also need:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

in your android manifest.

Edit:

Note that having an active network interface doesn't guarantee that a particular networked service is available. Network issues, server downtime, low signal, captive portals, content filters and the like can all prevent your app from reaching a server. For instance you can't tell for sure if your app can reach Twitter until you receive a valid response from the Twitter service.

like image 163
Alex Jasmin Avatar answered Sep 28 '22 09:09

Alex Jasmin


I check for both Wi-fi and Mobile internet as follows...

private boolean haveNetworkConnection() {     boolean haveConnectedWifi = false;     boolean haveConnectedMobile = false;      ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);     NetworkInfo[] netInfo = cm.getAllNetworkInfo();     for (NetworkInfo ni : netInfo) {         if (ni.getTypeName().equalsIgnoreCase("WIFI"))             if (ni.isConnected())                 haveConnectedWifi = true;         if (ni.getTypeName().equalsIgnoreCase("MOBILE"))             if (ni.isConnected())                 haveConnectedMobile = true;     }     return haveConnectedWifi || haveConnectedMobile; } 

Obviously, It could easily be modified to check for individual specific connection types, e.g., if your app needs the potentially higher speeds of Wi-fi to work correctly etc.

like image 24
Squonk Avatar answered Sep 28 '22 11:09

Squonk