title: detecting internet connection in android
body: <p>I have an app in android which is a sort of a client side of a TCP/IP connection ...this app has the use of receiving GPSt  data from a GPS provider and sending it to the server side of my TCP/IP connection.</p>
  
  <p>Only that when there is no internet connection the GPS data I have to store it in a DB....and as soon as I have again internet connection I have to start again the client side and reconnect to my server and send the data.</p>
  
  <p>Question:1.How could I detect internet connection in android?(my app is running on emulator)</p>
  
  <p>2.Is possible,as soon as I detect internet connection to ask to my thread client to reconnect to the server???</p>
  
  <p>Here is a scheme of my code:</p>
  
  <pre><code>public void onCreate(Bundle savedInstanceState) {
  
      super.onCreate(savedInstanceState);
  
      setContentView(R.layout.client);
  
      lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  
      locationListener = new MyLocationListener();
  
  
  
      cThread = new Thread(new ClientThread(syncToken));
  
      cThread.start();
  
      lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
              locationListener);
  }
  </code></pre>
  
  <hr>
  
  <pre><code>public void onLocationChanged(Location loc) {
          if (loc != null) {
  
                      latitude = (int) (loc.getLatitude() * 1E6);
  
                      longitude = (int) (loc.getLongitude() * 1E6);
  
  
                      }
                  }
  
  
  
              GeoPoint p = new GeoPoint(latitude, longitude);
              // geoPointsArray.add(p);
              db.insertData1(longitude, latitude);
  
  
  
  
          }
  </code></pre>
  
  <hr>
  
  <pre><code>public class ClientThread implements Runnable {
      Object syncToken;
  
  
  
      public void run() {
          try {
              InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
  
              socket = new Socket(serverAddr, 7001);
  
              Log.d(" ", "Clientul s-a conect");
  
          } catch (UnknownHostException e) {
  
              System.err.println("Don't know about host");
  
          } catch (IOException e) {
  
              System.err
                      .println("Couldn't get I/O for the connection to host");
          }
  
          try {
  
              os = new ObjectOutputStream(socket.getOutputStream());
  
          } catch (IOException e) {
  
  
              System.out.println(e);
          }
  
          while (true) {
  
  
              synchronized (syncToken) {
  
                  try {
                      syncToken.wait();
  
                  } catch (InterruptedException e) {
  
                      e.printStackTrace();
  
                  }
              }
  
              if (socket != null &amp;&amp; os != null) {
  
                      try {
  
              //send data through the socket
  
                      }catch (Exception e){
  
                          e.printStackTrace();
  
                      }
  
                      i++;
  
                  }
  
              }
  
  
      }
  }
  </code></pre>
  
  <hr>
  
  <p>EDIT:</p>
  
  <p>Here is what I did:</p>
  
  <pre><code>private NetworkStateReceiver mNetSateReceiver = null;
  
  private class NetworkStateReceiver extends BroadcastReceiver
  
  {
      @Override
  
      public void onReceive( Context context, Intent intent )
  
      {
          ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  
          NetworkInfo info =connectivity.getActiveNetworkInfo();;
  
          if (info != null &amp;&amp; info.isConnectedOrConnecting()) {
  
  
              System.out.println(" internet connection!");
  
  
          } 
          else
              System.out.println("no internet connection");
  
  
  
      }
  }
  </code></pre>
  
  <hr>
  
  <pre><code>onCreate(){
  
  
  
       registerReceiver( mNetSateReceiver, new IntentFilter(
                  ConnectivityManager.CONNECTIVITY_ACTION ) );
  
          syncToken = new Object();
  
          cThread = new Thread(new ClientThread(syncToken));
  
          cThread.start();
  
  
  }
  </code></pre>
  
  <hr>
  
  <pre><code> public void onDestroy(){ 
  
       super.onDestroy(); 
  
       db.close();
  
          unregisterReceiver( mNetSateReceiver );
   }
  </code></pre>
  
  <hr>
  
  <p>I understand that everytime my state connection changes my onReceive() gets called....that means that I should start my thread in onReceive() when there is internet conection????</p>
  
  <p>I'm a little bit confuse if u could clear up a little bit the thing for me...and where should I start that Intent that u are telling me??
  Thx</p>