Click here to Skip to main content
15,617,231 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi friends,

I developed a android application with web-view control. When i open the application then it will load the some web-application with in my android application. If my mobile don't have any internet connection then it will display a message like "Web page not available ...... some thing some thing".

But my requirement is when i open the application, it should check the internet connection status. If the connection is available then it allow the application to perform all operations. If the connection is not available then it should display the toast message with "no internet connection" and when i click on OK in toast message then i should close the application. So please provide me a code for my requirement

Thanks in advance friends.
Updated 9-Mar-14 21:24pm

It's pretty simple. See :
public boolean isConnectingToInternet(){
        ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
          if (connectivity != null)
              NetworkInfo[] info = connectivity.getAllNetworkInfo();
              if (info != null)
                  for (int i = 0; i < info.length; i++)
                      if (info[i].getState() == NetworkInfo.State.CONNECTED)
                          return true;

          return false;

Call this method on the launcher activity of your application, right after setContentView().
If it is returning true then there you go !

src : androidhive[^]

Share this answer
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900