Android Coding Example: Checking for the presence of an Internet connection on an Android device (Updated 17.4.2013)

This article presents some code to check for the presence of any Internet connection on an Android handset.

The code works for firmwares 1.5 – 4.2.2

Your AndroidManifest.xml will need to include the following permission;

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

The following is a revised shortened version of the previous code in this post.

    public final boolean isInternetOn() {
        Log.d(APP_TAG, ACT_TAG + "Checking connectivity ...");
        ConnectivityManager connec = null;
        connec =  (ConnectivityManager)getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
        if ( connec != null){
            Log.d(APP_TAG, ACT_TAG + "We have internet");
            NetworkInfo result = connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            if (result != null && result.isConnectedOrConnecting());
            return true;
        Log.d(APP_TAG, ACT_TAG + "No internet connection found");
        return false;

The following code demonstrates how to call the above method;

if(isInternetOn()) {
return true;
return false;

Please note, to test wifi connectivity on the emulator you will need to press F8 to enable/disable the 3G. Thanks to houcine for this tip.

  1. #1 by Chan on November 15, 2010 - 11:05 am

    :-) Hi,

    Even i used something similar to above mentioned code.. it worked fine in real device..but in emulator, even after unplugging the network cable to the system, i could able to get “connection available” message..

    Please let me know is there any way which works in both emulator and device in this case..

  2. #2 by houcine on February 22, 2011 - 10:50 am


    if you want to test the code above on your emulator , press f8 to enable/disable the network 3G ,

    i already test it , it works

    Good luck

    • #3 by jay on September 11, 2012 - 12:48 pm

      thanks for f8

  3. #5 by raj on September 26, 2011 - 10:19 am

    connec.getNetworkInfo(0).getState(); when ever i execute this code i got an error “the application stopped unexpectedly please try again force close”. please tel me y?

    • #6 by ChrisD on September 26, 2011 - 12:31 pm

      Hi Raj,

      Are you debugging this code in the emulator?

      If so, it won’t work I’m afraid. It has to be a physical phone.


    • #7 by Vinay on September 27, 2011 - 10:21 am

      The ConnectivityManager requires a special permission to be added to the Manifest.

      Please add this line to AndroidManifest.xml

      • #8 by Vinay on September 27, 2011 - 10:23 am

        • #9 by Vinay on September 27, 2011 - 10:24 am

          The comment system won’t let me paste code, I guess. The permission required is:

  4. #10 by ChrisD on September 27, 2011 - 10:34 am

    Thanks for the comments.
    I have now updated the article to include the AndroidManifest.xml permissions entry. I have also added houcine’s tip on testing the code within the emulator.

    Hopefully this will help those that find the article in future.

  5. #11 by Ramkumar on November 19, 2011 - 9:18 am

    Thanks for your comments.. I am running my aps in background service. is it possible to dissconnect all networks when it runs in Service and when Service stops all networks(Bluetooth,wifi) are to be enable?

  6. #12 by Nauman on December 2, 2011 - 5:39 pm

    I followed above method but getting an error of
    force close and stopped unexpectedly. I am try to execute it on an emulator. what i am doing wrong please help me.
    uses permission is also added in Menifest file

  7. #13 by Wan Mat on December 23, 2011 - 7:29 am

    Thank you very much for the nice example. It’s work fine in my Motorola XOOM and Samsung Galaxy Mini (GT-S5570).

  8. #14 by Mai Phuong Lan on January 16, 2012 - 2:56 am

    i think we can rewrite the method so that its more simple:
    public final boolean isInternetOn() {
    ConnectivityManager connec = (ConnectivityManager)getSystemService(CONNECTIVITY_SERVICE);
    if ( connec.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED || connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED )
    return false;
    return true;

  9. #15 by Mai Phuong Lan on January 16, 2012 - 2:58 am
    this is my first app.
    i think i love programming android app though my skill is not so good

  10. #16 by Sachin Ambalkar on January 27, 2012 - 10:07 am

    thanksss……..code is working!!!!!!!!!!!!!!!!!!

  11. #17 by sanu on February 15, 2012 - 7:34 am

    its even return true if no gprs connection is avail and only network connection(but no internet connection) is avail through Wifi…..

  12. #18 by WM on February 22, 2012 - 3:50 am

    Thanks. I’ve managed to get it to work with just State.CONNECTED instead of NetworkInfo.State.CONNECTED. Finally, managed to find one a beginner like me could understand. Keep up the awesome work.

  13. #19 by Green Stickman on March 22, 2012 - 4:04 am

    Thank you very much for this information. I’ve been looking for this for 2 weeks now.

    I appreciated it!

    • #20 by ChrisD on March 22, 2012 - 9:20 am

      Hey Green,

      Many thanks for taking the time to leave a comment.

      I’m pleased I have helped you in some way.


  14. #21 by abhijit gujar on April 18, 2012 - 4:50 pm

    Note :just in case some ppl might not know .if you are testing on emulator connect and disconnect internet using ” F8 ” and not by disconnecting your actual internet .

  15. #22 by emy on April 27, 2012 - 7:05 am

    thank for that code …
    but i have an error in NetworkInfo.State.CONNECTING i change it to State.CONNECTING and it has not any error but when i run the application it doesn’t get anything there is no effect when press f8 ???!!!!
    plz help me in that if you don’t mind

  16. #23 by Prateek on May 11, 2012 - 4:19 pm

    hats off to u houcine for ur F8 tip.

  17. #24 by chandramohan sahu on July 6, 2012 - 11:01 am

    thanx for give the code i am using this code . code run successfully.

  18. #25 by priya on July 6, 2012 - 2:41 pm

    is this code also work to check the wifi connection?

  19. #26 by Dany Wehbe on August 17, 2012 - 9:36 pm

    thank you so much,I was searching all over the net for 2 days,but your solution was the best and it is the only worked code,thanks again and I hope you continue your good work benefiting others,my greetings

  20. #27 by george on November 8, 2012 - 10:47 am

    thanks so much for the F8 tip

  21. #28 by Dowds on November 24, 2012 - 4:21 pm

    Hey I am having trouble with my manifest:
    I put the permission

    between the close of application and the close of manifest yet I am getting this error.] Unable to read C:\Users\Dowdall\workspace\cellular\AndroidManifest.xml: org.eclipse.core.internal.resources.ResourceException: Resource is out of sync with the file system: ‘/cellular/AndroidManifest.xml’.

    Can anyone help?! Thank you

  22. #29 by Dam Syndicate on December 17, 2012 - 5:43 pm

    thanks very much for the code has been very useful for my project and it isn’t deprecated ;).

    • #30 by ChrisD on December 18, 2012 - 9:05 am

      Hey Dam,

      Many thanks for leaving a comment I’m pleased the code has helped you.

      Merry Christmas!


  23. #31 by Andrew on June 26, 2013 - 2:39 pm

    Hi Christian,

    Are you sure that in line:

    if (result != null && result.isConnectedOrConnecting());

    code must end with a semi-colon, or is it a typo?

  24. #32 by Kushi on July 12, 2013 - 8:31 pm

    Superb example man. really helpful.
    We can get some more additional info about the internet connection,
    Have a look at this example,

(will not be published)

Switch to our mobile site