Ping in Android from your code.
- Does NOT invoke CLI ping command.
- Support for IPv4 and IPv6.
Implementation notes:
- Listener is executed on calling Thread (Not UI Thread).
- Requires Lollipop or greater.
- As always, don't do I/O on the UI Thread.
- Does NOT appear to work with the emulator, make sure you use a real device.
See the MainActivity for more.
final InetAddress dest = InetAddress.getByName(mHost);
final Ping ping = new Ping(dest, new Ping.PingListener() {
@Override
public void onPing(final long timeMs, final int count) {
Log.d(TAG, "#" + count + " ms: " + timeMs + " ip: " + dest.getHostAddress(), null);
}
@Override
public void onPingException(final Exception e, final int count) {
Log.e(TAG, "#" + count + " ip: " + dest.getHostAddress(), e);
}
});
AyscTask.THREAD_POOL_EXECUTOR.execute(ping);
You can bind the ping to a Network (e.g. WiFi or Mobile). See MainActivity for more.
final Ping ping = new Ping(inetAddress, listener);
ping.setNetwork(network);
AyscTask.THREAD_POOL_EXECUTOR.execute(ping);
gradlew lib:assembleRelease