The following code may be used to ping the IP / Domain addresses and to obtain latency
Tested in Android 4.4.2, 6.0
Mobile handsets: Lenovo A6000, Redmi Note 4A
Working application: PUBG Network Quality Check
Minimum API Tested: 19
public long pingg (String domain){
Runtime runtime = Runtime.getRuntime();
try {
long a = (System.currentTimeMillis() % 100000);
Process ipProcess = runtime.exec("/system/bin/ping -c 1 "+domain);
ipProcess.waitFor();
long b = (System.currentTimeMillis() % 100000);
if (b <= a) {
timeofping = ((100000 - a) + b);
} else {
timeofping = (b - a);
}
}catch (Exception e){
}
return timeofping;
}
Explanation:
The code fetches the current time in milliseconds to a variable.
The ping function is then called, with the domain as the parameter.
The code waits till the pinging is over.
The current time in milliseconds is fetched again.
The time interval between this is calculated as latency.
Special Thanks to StackOverflow for the idea and code concept !