0

I want to check the Internet connection available or not in device so how can i got this..please give me code for this..

thank a lot In advance

Heiko Rupp
  • 30,426
  • 13
  • 82
  • 119
Hardik Gajjar
  • 5,038
  • 8
  • 33
  • 51

5 Answers5

3

hey buddy...apply this code..it might be helpful to u.thanks in advance....

boolean connected;

    private boolean checkInternetConnection()
    {

        ConnectivityManager conMgr = (ConnectivityManager) getSystemService (Context.CONNECTIVITY_SERVICE);

        // ARE WE CONNECTED TO THE NET

        if (conMgr.getActiveNetworkInfo() != null

                && conMgr.getActiveNetworkInfo().isAvailable()

                && conMgr.getActiveNetworkInfo().isConnected()) 
        {

        return true;

        }
        else 
        {
        return false;

        }
    }
Nirav Bhandari
  • 4,550
  • 6
  • 32
  • 59
2
        ConnectivityManager connectionService =
            (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
        if (connectionService.getActiveNetworkInfo().isConnectedOrConnecting()) {
            // Device is online
        }
Jim Blackler
  • 22,946
  • 12
  • 85
  • 101
1
// Network is connected or not 
public boolean isConnected()
{
    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)
                  {
                      Log.d("LOG","Network is Available");
                      return true;
                  }

      }
      return false;
}
Virag Brahme
  • 2,062
  • 1
  • 20
  • 32
1

Use the following function inorder to check the internet connction:

public boolean isOnline()
    {

         ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
         NetworkInfo ni = cm.getActiveNetworkInfo();
         boolean result = false;
         if(ni != null )
         {
             if(  ni.getState() == NetworkInfo.State.CONNECTED )
             {
                 result = true;
             }
         }

         return result;

        } 

check what isOnline() returns .If true then Internet is connected else Inernet is not connected . Hope this will help you..:)

Dinesh Sharma
  • 11,533
  • 7
  • 42
  • 60
0

Use this Answer It`s help.

public boolean isOnline() {
  ConnectivityManager cm =(ConnectivityManager)      
  getSystemService(Context.CONNECTIVITY_SERVICE);
  NetworkInfo netInfo = cm.getActiveNetworkInfo();
  if (netInfo != null && netInfo.isConnectedOrConnecting()) {
     return true;
  }
  return false;
}

if(temp==true){
    genHelper.showToast("Net Connection");              
}else{              
    genHelper.showToast(" Not  Connected");
}
Youddh
  • 1,511
  • 4
  • 34
  • 51
Zala Janaksinh
  • 2,929
  • 5
  • 32
  • 58