How To Check Internet Connection In Your Android Application

To check internet connection in your android application should be your first priority. In this example I’ll show you how to you can check internet connection in a simple way. Internet connection check code should be placed in your first activity which runs first. So that you can handle it if there is no internet.

Take an example of live scenario say that user forgot to turn on his/her internet connection and he/she has newly installed your application hence he/she don’t know if this application needs internet connection. In this case there should be pop up saying that you need to turn on your internet connection. Now the user know that this application need internet and hence turns on the internet connection in advance. Even it would be better if you show direct button for turning internet on and off. So that user can easily turn on the internet connection.

Creating User Interface

You need a simple layout with one button. So that on click of that it checks if its connected to internet or not.

activity_main.xml

android internet check

Building Android Application

  • Define the view Object in MainActivity.java.

  • Now define the button on click event.

  • Now define the function that check internet connection.

  • Full code of MainActivity.java.

  • Now you can run the application.

                        android internet check  android internet check android internet check

That all my friends if you have any problem running this code then do comment I will be happy to help you.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.