-1

Is it possible to check if the phone has access to internet by trying to connect to a url? If it succeed to connect to the website the application will run normally and if it fails to connect, an alertdialog will pop up telling the user the problem.

Carlj901
  • 1,329
  • 4
  • 24
  • 39
  • possible duplicate of [Android internet connectivity check problem](http://stackoverflow.com/questions/2753412/android-internet-connectivity-check-problem) – bzlm Dec 14 '11 at 11:16
  • 1
    [You should do some search before posting a question](http://www.google.com.pk/search?q=android+check+if+network+is+available) – Adil Soomro Dec 14 '11 at 11:17
  • check this [How to check internet access on Android? InetAddress never timeouts…](http://stackoverflow.com/questions/1560788/how-to-check-internet-access-on-android-inetaddress-never-timeouts/35859366#35859366) – Vishnu M A Mar 08 '16 at 05:10

4 Answers4

4

Checking the internet will not guarantee you that you reach to your site without any hassles. See, basically even if there is an internet connection, things can still go wrong. Having an active network interface doesn't guarantee that a particular networked service is available. Networks 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.

However you can do the following.

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

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

You will also need:

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

in your android manifest.

Kebman
  • 1,901
  • 1
  • 20
  • 32
Jigar Pandya
  • 6,004
  • 2
  • 27
  • 45
  • Thing is, I want to check if the phone is able to connect to that specific URL and not if I have an internet connection. Sorry if I didn't make that clear. – Carlj901 Dec 14 '11 at 12:43
  • To make this happen; please create one webservice that returns true as data; make URL request to this service and if you get true it means you can reach to the destination; in catch you should return false so if you get false in return you can consider you are not able to connect to your server. Let me know if this is helpfull. – Jigar Pandya Dec 14 '11 at 13:08
2

This is one way (if you searched a bit I am sure you would have reached it)

private boolean isNetworkAvailable() {

    ConnectivityManager cm
          = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ai= cm.getActiveNetworkInfo();
    return ai!= null;
}

Another way would be simply to surround your request with a try...catch and catch an IOException.

The problem is that this method will block for some time. You can add in the catch code to show an alertDialog

Sherif elKhatib
  • 45,786
  • 16
  • 89
  • 106
1

Try this one -

if (!isNetworkAvailable())
     {
         //No network available       
     } 


private boolean isNetworkAvailable() {
            ConnectivityManager connectivityManager 
                  = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
            return activeNetworkInfo != null;
        }
Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
0

mention the permission in your manifest.xml for Internet and use the followinf code

private boolean isNetworkAvailable() {
            ConnectivityManager connectivityManager 
                  = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
            return activeNetworkInfo != null;
        }
Gaurav
  • 114
  • 3
  • 7