Android

Ping ICMP

Introduction#

The ICMP Ping request can be performed in Android by creating a new process to run the ping request. The outcome of the request can be evaluated upon the completion of the ping request from within its process.

Performs a single Ping

This example attempts a single Ping request. The ping command inside the runtime.exec method call can be modified to any valid ping command you might perform yourself in the command line.

try {
    Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
    int exitValue = ipProcess.waitFor();
    ipProcess.destroy();
    
    if(exitValue == 0){
        // Success
    } else { 
        // Failure
    }
} catch (IOException | InterruptedException e) {
    e.printStackTrace();
}

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow