title: check ethernet connection periodically android
body: <p>I'm trying to periodically check the network connection. However, this is for a Chinese Android Mini PC, not a tablet or smartphone. I'm using an <em>ethernet to usb adapter</em> instead of Wi-Fi. First I used a broadcastreceiver class:</p>
  
  <pre><code>public class NetworkStateReceiver extends BroadcastReceiver {
  
      @Override
      public void onReceive(Context context, Intent intent) {
          if (intent.getExtras() != null) {
              @SuppressWarnings("deprecation")
              NetworkInfo eni = (NetworkInfo) intent.getExtras().get(
                      ConnectivityManager.EXTRA_NETWORK_INFO);
  
              if (eni != null &amp;&amp; eni.getState() == NetworkInfo.State.CONNECTED) {
                  Log.d(TAG, "Network " + eni.getTypeName() + " connected.");
              }
          }
          if (intent.getExtras().getBoolean(
                  ConnectivityManager.EXTRA_NO_CONNECTIVITY, Boolean.FALSE)) {
              Log.d(TAG, "There's no network connectivity.");
          }
      }
  }
  </code></pre>
  
  <p>This works perfectly for Wi-Fi and mobile. However, for ethernet, there are complications. When I connect the <em>ethernet to usb adapter</em>, it thinks it <strong>already</strong> has ETHERNET connection, <strong>whether the ethernet cable is connected or not</strong>. Only when removing the adapter, it knows the ethernet connection was removed.</p>
  
  <p>I tried using a socket, and this kind of works:</p>
  
  <pre><code>    private static boolean checkSocket(String host, int port) {
          Socket socket = null;
          boolean reachable = false;
          try {
              socket = new Socket(InetAddress.getByName(host), port);
              reachable = true;
          } catch (UnknownHostException e) {
          } catch (IOException e) {
          } finally {
              if (socket != null) {
                  try {
                      socket.close();
                  } catch (IOException e) {
                  }
              }
          }
          return reachable;
      }
  </code></pre>
  
  <p>When there is a connection, it works perfectly and fast. When the connection is lost, it takes way too long for the program to know it has. I need this solution, but it should know <strong>way faster</strong> that the ethernet connection has been lost. Also, this relies on Exceptions, which I'm not fond of at all.</p>
  
  <p>Lastly I tried a simple ICMP message:</p>
  
  <pre><code>    try {
          InetAddress address = InetAddress.getByName(host);
          if (address.isReachable(timeout)) {
              return true;
          }
      } catch (UnknownHostException e) {
      } catch (IOException e) {
      }
      return false;
  </code></pre>
  
  <p>This <em>should</em> work, right? Unfortunately, it doesn't. Until now, I've always received a <code>false</code> when executing this code.</p>
  
  <p>What am I doing wrong and what is the correct way to do this? </p>
  
  <p><strong>EDIT 1</strong></p>
  
  <p>I have now tried this solution, which works and doesn't work. It's funny and annoying, as I'm checking this in the <code>onResume()</code>. After a few correct tries, it suddenly stops. I have no idea why though.</p>
  
  <pre><code>    boolean reachable = false;
      try {
          Process p1 = java.lang.Runtime.getRuntime().exec("ping -c 1 " + host);
          int retValue = p1.waitFor();
          reachable = (retValue == 0);
          Log.d(TAG, String.valueOf(reachable));
          p1.destroy();
      } catch (IOException e) {
          e.printStackTrace();
      } catch (InterruptedException e) {
          e.printStackTrace();
      }
      return reachable;
  </code></pre>